diff --git a/src/src/extension/PrimitiveExtension.cs b/src/src/extension/PrimitiveExtension.cs index b4f3339..c6acf2c 100644 --- a/src/src/extension/PrimitiveExtension.cs +++ b/src/src/extension/PrimitiveExtension.cs @@ -98,8 +98,7 @@ internal static byte[] ToPrimitive(this T value, Type type) } else if (type.IsArray || value is T[]) { - // TODO: array - // primitive.Add.Array(value); + primitive.Add.Array(value); } else if (value is ICollection) { diff --git a/src/src/primitive/interfances/IPrimitiveAdd.cs b/src/src/primitive/interfances/IPrimitiveAdd.cs index 9bf6319..d3a0aa3 100644 --- a/src/src/primitive/interfances/IPrimitiveAdd.cs +++ b/src/src/primitive/interfances/IPrimitiveAdd.cs @@ -36,9 +36,12 @@ public interface IPrimitiveAdd void Class(T value); void Struct(T value); void Array(T[] value); - void List(object value); void List(List value); void BigInteger(BigInteger value); void Bytes(byte[] value); + + // overhead + void Array(object value); + void List(object value); } } \ No newline at end of file diff --git a/src/src/primitive/partials/PrimitiveAdd.cs b/src/src/primitive/partials/PrimitiveAdd.cs index bd11411..fc242ec 100644 --- a/src/src/primitive/partials/PrimitiveAdd.cs +++ b/src/src/primitive/partials/PrimitiveAdd.cs @@ -267,6 +267,41 @@ public void Array(T[] value) } } + public void Array(object value) + { + if (value == null) return; + var type = value.GetType(); + if (!type.IsArray) return; + var childrenType = type.GetElementType(); + if (childrenType == null) return; + + var list = (IList)value; + + Vault.Add(Prefix.Array); + + var size = list.Count; + + if (size > 0) + { + var collection = new List(); + + foreach (var x in list) + { + var result = x.ToPrimitive(childrenType); + collection.AddRange(result); + } + + Vault.AddRange(BitConverter.GetBytes(size)); // objects count + Vault.AddRange(BitConverter.GetBytes(collection.Count)); // buffer size + Vault.AddRange(collection); // buffer + } + else + { + Vault.AddRange(BitConverter.GetBytes(0)); // objects count + Vault.AddRange(BitConverter.GetBytes(0)); // buffer + } + } + public void List(object value) { if (value == null || !(value is ICollection list)) return; diff --git a/test/ByterTest.csproj b/test/ByterTest.csproj index 16c6122..2327f70 100644 --- a/test/ByterTest.csproj +++ b/test/ByterTest.csproj @@ -10,7 +10,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/test/primitive/ReadAndWrite.cs b/test/primitive/ReadAndWrite.cs index 5b16b88..e435024 100644 --- a/test/primitive/ReadAndWrite.cs +++ b/test/primitive/ReadAndWrite.cs @@ -591,15 +591,18 @@ public void TestArrayFromStruct2() p.Add.Struct(real); + Assert.Equal(0, p.Position); + + var clone = p.Get.Struct(); - Assert.True(p.IsValid); Assert.Equal(real.Array, clone.Array); for (int i = 0; i < real.Array.Length; i++) { Assert.Equal(real.Array[i], clone.Array[i]); } - + + Assert.True(p.IsValid); Terminate(ref p); }