From fb86a01ed54e928014d4d30196839d5785a8aaa8 Mon Sep 17 00:00:00 2001 From: Alecio Furanze Date: Tue, 28 May 2024 10:30:03 +0200 Subject: [PATCH] improve PrimitiveAdd->Array(Object) --- src/src/primitive/partials/PrimitiveAdd.cs | 11 +++++++---- test/primitive/ReadAndWrite.cs | 15 ++++----------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/src/primitive/partials/PrimitiveAdd.cs b/src/src/primitive/partials/PrimitiveAdd.cs index 9b860f0..fc242ec 100644 --- a/src/src/primitive/partials/PrimitiveAdd.cs +++ b/src/src/primitive/partials/PrimitiveAdd.cs @@ -274,9 +274,8 @@ public void Array(object value) if (!type.IsArray) return; var childrenType = type.GetElementType(); if (childrenType == null) return; - var genericType = typeof(List<>).MakeGenericType(childrenType); - var list = Activator.CreateInstance(genericType) as IList; - if (list == null) return; + + var list = (IList)value; Vault.Add(Prefix.Array); @@ -286,7 +285,11 @@ public void Array(object value) { var collection = new List(); - foreach (var x in list) collection.AddRange(x.ToPrimitive()); + 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 diff --git a/test/primitive/ReadAndWrite.cs b/test/primitive/ReadAndWrite.cs index 791b5ea..e435024 100644 --- a/test/primitive/ReadAndWrite.cs +++ b/test/primitive/ReadAndWrite.cs @@ -592,24 +592,17 @@ public void TestArrayFromStruct2() p.Add.Struct(real); Assert.Equal(0, p.Position); - - // 1 struct prefix - // 1 array prefix - // 4 array elements - // 4 array buffer size - // 12 array buffer (4*3) - // total 22 - Assert.Equal(22, p.Data.Length); - + + 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); }