Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ObjectInfo.cs
1using System;
2using System.Collections.Generic;
5
7{
11 internal class ObjectInfo
12 {
13 private Dictionary<string, PropertyReference> sorted = null;
14 public PropertyReference[] Properties;
15 public Type Type;
16
22 public void Serialize(Serializer Output, object Object)
23 {
24 if (Object is null)
25 Output.WriteByte(0);
26 else
27 {
28 Type T = Object.GetType();
29 ObjectInfo Info = this;
30
31 if (T == this.Type)
32 Output.WriteString(string.Empty);
33 else
34 {
35 Output.WriteString(T.FullName);
36
37 IProperty P = ClusterEndpoint.GetProperty(T);
38 if (P is ObjectProperty _)
39 Info = ClusterEndpoint.GetObjectInfo(T);
40 else
41 {
42 P.Serialize(Output, Object);
43 return;
44 }
45 }
46
47 foreach (PropertyReference Property in Info.Properties)
48 {
49 Output.WriteString(Property.Name);
50 object Value = Property.Info.GetValue(Object);
51 Property.Property.Serialize(Output, Value);
52 }
53
54 Output.WriteByte(0);
55 }
56 }
57
65 public object Deserialize(Deserializer Input, Type ExpectedType)
66 {
67 string TypeName = Input.ReadString();
68 ObjectInfo Info;
69
70 if (TypeName is null)
71 return null;
72 else if (string.IsNullOrEmpty(TypeName))
73 Info = this;
74 else
75 {
76 Type T = Types.GetType(TypeName);
77 if (T is null)
78 throw new KeyNotFoundException("Type name not recognized: " + TypeName);
79
80 IProperty P = ClusterEndpoint.GetProperty(T);
81 if (P is ObjectProperty _)
82 Info = ClusterEndpoint.GetObjectInfo(T);
83 else
84 return P.Deserialize(Input, T);
85 }
86
87 if (Info.sorted is null)
88 {
89 Dictionary<string, PropertyReference> Sorted = new Dictionary<string, PropertyReference>();
90
91 foreach (PropertyReference P in Info.Properties)
92 Sorted[P.Name] = P;
93
94 Info.sorted = Sorted;
95 }
96
97 object Result = Activator.CreateInstance(Info.Type);
98 string PropertyName = Input.ReadString();
99
100 while (!string.IsNullOrEmpty(PropertyName))
101 {
102 if (!Info.sorted.TryGetValue(PropertyName, out PropertyReference P))
103 throw new KeyNotFoundException("Property Name not found: " + Info.Type.FullName + "." + PropertyName);
104
105 P.Info.SetValue(Result, P.Property.Deserialize(Input, P.Property.PropertyType));
106
107 PropertyName = Input.ReadString();
108 }
109
110 return Result;
111 }
112
113 }
114}
Represents one endpoint (or participant) in the network cluster.
string ReadString()
Reads a string from the input.
Definition: Deserializer.cs:99
abstract void Serialize(Serializer Output, object Value)
Serializes the property value of an object.
void WriteString(string Value)
Writes a string to the output.
Definition: Serializer.cs:118
void WriteByte(byte Value)
Writes a byte to the output.
Definition: Serializer.cs:54
Static class that dynamically manages types and interfaces available in the runtime environment.
Definition: Types.cs:14
static Type GetType(string FullName)
Gets a type, given its full name.
Definition: Types.cs:41
object Deserialize(Deserializer Input, Type ExpectedType)
Deserializes the property value
void Serialize(Serializer Output, object Value)
Serializes the property value of an object.