Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PropertyDefinition.cs
1using System;
2using System.Collections.Generic;
3using System.Reflection;
4using System.Threading.Tasks;
5using Waher.Events;
9
11{
15 public enum PropertyType
16 {
20 Label,
21
25 External
26 }
27
31 [TypeName(TypeNameSerialization.None)]
32 public class PropertyDefinition
33 {
34 private readonly Dictionary<string, KeyValuePair<PropertyInfo, FieldInfo>> memberInfo = new Dictionary<string, KeyValuePair<PropertyInfo, FieldInfo>>();
35 private IPropertyEvaluator evaluator;
36 private bool initialized;
37
42 {
43 }
44
49 public PropertyDefinition(string Label)
50 {
51 this.ExternalSource = null;
52 this.Definition = Label;
53 this.Type = PropertyType.Label;
54 }
55
62 {
63 this.ExternalSource = ExternalSource;
64 this.Definition = Definition;
65 this.Type = PropertyType.External;
66 }
67
73 public static PropertyDefinition[] ToArray(string[] PropertyNames)
74 {
75 int i, c = PropertyNames.Length;
76 PropertyDefinition[] Result = new PropertyDefinition[c];
77
78 for (i = 0; i < c; i++)
79 Result[i] = new PropertyDefinition(PropertyNames[i]);
80
81 return Result;
82 }
83
87 public string Definition { get; set; }
88
92 [DefaultValueStringEmpty]
93 public string ExternalSource { get; set; }
94
98 public PropertyType Type { get; set; }
99
105 public async Task<object> GetValue(object Instance)
106 {
107 if (Instance is null)
108 return null;
109
110 Type T;
111
112 switch (this.Type)
113 {
114 case PropertyType.Label:
115 KeyValuePair<PropertyInfo, FieldInfo> P;
116
117 T = Instance.GetType();
118
119 lock (this.memberInfo)
120 {
121 if (!this.memberInfo.TryGetValue(T.FullName, out P))
122 {
123 P = new KeyValuePair<PropertyInfo, FieldInfo>(
124 T.GetRuntimeProperty(this.Definition),
125 T.GetRuntimeField(this.Definition));
126
127 this.memberInfo[T.FullName] = P;
128 }
129 }
130
131 if (!(P.Key is null))
132 {
133 if (P.Key.DeclaringType.IsAssignableFrom(Instance.GetType()))
134 return P.Key.GetValue(Instance);
135 }
136 else if (!(P.Value is null))
137 {
138 if (P.Value.DeclaringType.IsAssignableFrom(Instance.GetType()))
139 return P.Value.GetValue(Instance);
140 }
141 break;
142
143 case PropertyType.External:
144 if (!this.initialized)
145 {
146 this.initialized = true;
147
148 T = Types.GetType(this.ExternalSource);
149 if (!(T is null))
150 {
151 this.evaluator = Types.Create(true, T) as IPropertyEvaluator;
152 if (!(this.evaluator is null))
153 await this.evaluator.Prepare(this.Definition);
154 }
155
156 }
157
158 if (!(this.evaluator is null))
159 {
160 try
161 {
162 return await this.evaluator.Evaluate(Instance);
163 }
164 catch (Exception ex)
165 {
166 Log.Exception(ex);
167 return null;
168 }
169 }
170 break;
171 }
172
173 return null;
174 }
175
181 public async Task<object> GetValue(GenericObject Instance)
182 {
183 if (!this.initialized)
184 {
185 this.initialized = true;
186
187 switch (this.Type)
188 {
189 case PropertyType.Label:
190 break;
191
192 case PropertyType.External:
194 if (!(T is null))
195 {
196 this.evaluator = Types.Create(true, T) as IPropertyEvaluator;
197 if (!(this.evaluator is null))
198 await this.evaluator.Prepare(this.Definition);
199 }
200 break;
201 }
202 }
203
204 switch (this.Type)
205 {
206 case PropertyType.Label:
207 if (Instance.TryGetFieldValue(this.Definition, out object Value))
208 return Value;
209 break;
210
211 case PropertyType.External:
212 if (!(this.evaluator is null))
213 return await this.evaluator.Evaluate(Instance);
214 break;
215 }
216
217 return null;
218 }
219
220 }
221}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1647
PropertyDefinition(string ExternalSource, string Definition)
Defines an indexable property.
PropertyDefinition(string Label)
Defines an indexable property.
PropertyType Type
Type of property definition
async Task< object > GetValue(GenericObject Instance)
Gets the object to index.
static PropertyDefinition[] ToArray(string[] PropertyNames)
Creates an array of property definitions from an array of property names.
async Task< object > GetValue(object Instance)
Gets the object to index.
Generic object. Contains a sequence of properties.
bool TryGetFieldValue(string PropertyName, out object Value)
Gets the value of a field or property of the object, given its name.
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
static object Create(bool ReturnNullIfFail, Type Type, params object[] Arguments)
Returns an instance of the type Type . Creates an instance of a type. If the constructor requires arg...
Definition: Types.cs:1237
Task Prepare(string Definition)
Prepares the evaluator with its definition.
TypeNameSerialization
How the type name should be serialized.
PropertyType
Type of indexed property.