Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ArrayProperty.cs
1using System;
2using System.Collections.Generic;
3using System.Reflection;
5
7{
11 public class ArrayProperty : Property
12 {
13 private readonly Type arrayType;
14 private readonly Type elementType;
15 private readonly IProperty elementProperty;
16
20 internal ArrayProperty(Type ArrayType, Type ElementType, IProperty ElementProperty)
21 : base()
22 {
23 this.arrayType = ArrayType;
24 this.elementType = ElementType;
25 this.elementProperty = ElementProperty;
26 }
27
31 public override Type PropertyType => this.arrayType;
32
38 public override void Serialize(Serializer Output, object Value)
39 {
40 Array A = (Array)Value;
41
42 if (A is null)
43 Output.WriteVarUInt64(0);
44 else
45 {
46 Output.WriteVarUInt64((ulong)A.Length + 1);
47
48 foreach (object Element in A)
49 this.elementProperty.Serialize(Output, Element);
50 }
51 }
52
59 public override object Deserialize(Deserializer Input, Type ExpectedType)
60 {
61 ulong Len = Input.ReadVarUInt64();
62 if (Len == 0)
63 return null;
64
65 Len--;
66
67 if (Len > int.MaxValue)
68 throw new Exception("Array too long.");
69
70 int i, c = (int)Len;
71
72 Array Result = Array.CreateInstance(this.elementType, c);
73
74 for (i = 0; i < c; i++)
75 Result.SetValue(this.elementProperty.Deserialize(Input,this.elementType), i);
76
77 return Result;
78 }
79
80 }
81}
ulong ReadVarUInt64()
Reads a variable-length unsigned integer from the input.
Definition: Deserializer.cs:56
override void Serialize(Serializer Output, object Value)
Serializes the property value of an object.
override object Deserialize(Deserializer Input, Type ExpectedType)
Deserializes the property value
void WriteVarUInt64(ulong Value)
Writes a variable-length unsigned integer to the output.
Definition: Serializer.cs:63