Skip to content

Commit

Permalink
improve PrimitiveAdd->Array(Object)
Browse files Browse the repository at this point in the history
  • Loading branch information
alec1o committed May 28, 2024
1 parent b1dff05 commit fb86a01
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
11 changes: 7 additions & 4 deletions src/src/primitive/partials/PrimitiveAdd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object>;
if (list == null) return;

var list = (IList)value;

Vault.Add(Prefix.Array);

Expand All @@ -286,7 +285,11 @@ public void Array(object value)
{
var collection = new List<byte>();

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
Expand Down
15 changes: 4 additions & 11 deletions test/primitive/ReadAndWrite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ArrayFromStruct2>();

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);
}

Expand Down

0 comments on commit fb86a01

Please sign in to comment.