diff --git a/src/src/primitive/partials/PrimitiveAdd.cs b/src/src/primitive/partials/PrimitiveAdd.cs index 96dd122..22ed030 100644 --- a/src/src/primitive/partials/PrimitiveAdd.cs +++ b/src/src/primitive/partials/PrimitiveAdd.cs @@ -173,18 +173,26 @@ public void Class(T value) public void Struct(T value) { - var type = value.GetType(); + var type = value == null ? typeof(T) : value.GetType(); if (!(type.IsValueType && !type.IsEnum && !type.IsPrimitive)) throw new InvalidOperationException($"Only struct is accepted. {type} isn't allowed"); // if (!type.IsSerializable) throw new InvalidOperationException("Only serialized class is accepted"); Vault.Add(Prefix.Struct); - var cache = new List(); - var props = type.GetProperties(); + if (props.Length <= 0 || value == null) + { + Vault.Add(1); // error + return; + } + + Vault.Add(0); // success + + var cache = new List(); + if (props.Length <= 0) { return; diff --git a/src/src/primitive/partials/PrimitiveGet.cs b/src/src/primitive/partials/PrimitiveGet.cs index 655d642..52a5499 100644 --- a/src/src/primitive/partials/PrimitiveGet.cs +++ b/src/src/primitive/partials/PrimitiveGet.cs @@ -433,7 +433,8 @@ public object Struct(Type type) throw new InvalidOperationException("Only struct is accepted"); if (!IsValidPrefix(Prefix.Struct)) throw new InvalidDataException(); - + if (!IsValidObject()) return default; // empty or null data + var instance = Activator.CreateInstance(type); var props = type.GetProperties(); diff --git a/test/primitive/Primitive.Null.cs b/test/primitive/Primitive.Null.cs index b9b7ccb..aa612c5 100644 --- a/test/primitive/Primitive.Null.cs +++ b/test/primitive/Primitive.Null.cs @@ -43,13 +43,12 @@ public void Null() primitive.Add.Class(NullConfig.TheEmptyClass); primitive.Add.Class(NullConfig.TheNullClass); - /* primitive.Add.Struct(NullConfig.TheEmptyStruct); primitive.Add.Struct(NullConfig.TheNullStruct); primitive.Add.Enum(NullConfig.TheEmptyEnum); primitive.Add.Enum(NullConfig.TheNullEnum); - */ + // read _ = primitive.Get.Bool(); @@ -117,13 +116,11 @@ public void Null() _ = primitive.Get.Class(); // empty _ = primitive.Get.Class(); // null - /* _ = primitive.Get.Struct(); // empty _ = primitive.Get.Struct(); // null _ = primitive.Get.Enum(); // empty _ = primitive.Get.Enum(); // null - */ } public static class NullConfig