Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
NullableProperty.cs
1using System;
2using System.Reflection;
3
5{
10 {
11 private readonly Type nullableType;
12 private readonly Type elementType;
13 private readonly ConstructorInfo constructor;
14 private readonly PropertyInfo valueProperty;
15 private readonly IProperty element;
16
20 public NullableProperty(Type NullableType, Type ElementType, IProperty Element)
21 : base()
22 {
23 this.nullableType = NullableType;
24 this.elementType = ElementType;
25 this.element = Element;
26 this.constructor = null;
27
28 foreach (ConstructorInfo CI in this.nullableType.GetTypeInfo().DeclaredConstructors)
29 {
30 ParameterInfo[] P = CI.GetParameters();
31 if (P.Length == 1 && P[0].ParameterType == this.elementType)
32 {
33 this.constructor = CI;
34 break;
35 }
36 }
37
38 this.valueProperty = this.nullableType.GetRuntimeProperty("Value");
39 }
40
44 public override Type PropertyType => this.nullableType;
45
51 public override void Serialize(Serializer Output, object Value)
52 {
53 if (Value is null)
54 Output.WriteBoolean(false);
55 else
56 {
57 Output.WriteBoolean(true);
58 this.element.Serialize(Output, this.valueProperty.GetMethod.Invoke(Value, null));
59 }
60 }
61
68 public override object Deserialize(Deserializer Input, Type ExpectedType)
69 {
70 if (Input.ReadBoolean())
71 return this.element.Deserialize(Input, this.elementType);
72 else
73 return null;
74 }
75 }
76}
bool ReadBoolean()
Reads a boolean value from the input.
Definition: Deserializer.cs:38
override void Serialize(Serializer Output, object Value)
Serializes the property value of an object.
NullableProperty(Type NullableType, Type ElementType, IProperty Element)
Nullable property
override object Deserialize(Deserializer Input, Type ExpectedType)
Deserializes the property value
void WriteBoolean(bool Value)
Writes a boolean value to the output.
Definition: Serializer.cs:45