Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
PropertyOrder.cs
1using System;
2using System.Collections.Generic;
3using System.Reflection;
6
7namespace Waher.Script.Order
8{
12 public class PropertyOrder : IComparer<IElement>
13 {
14 private readonly ScriptNode node;
15 private readonly string name;
16 private readonly int sign;
17 private Type lastType = null;
18 private FieldInfo lastFieldInfo = null;
19 private PropertyInfo lastPropertyInfo = null;
20
27 public PropertyOrder(ScriptNode Node, string Name, int Sign)
28 {
29 this.node = Node;
30 this.name = Name;
31 this.sign = Sign;
32 }
33
40 public int Compare(IElement x, IElement y)
41 {
42 IElement v1 = this.GetValue(x);
43 IElement v2 = this.GetValue(y);
44
45 return this.sign * ElementOrder.Compare(v1, v2, this.node);
46 }
47
48 private IElement GetValue(IElement Obj)
49 {
50 object Value = Obj.AssociatedObjectValue;
51 Type T = Value.GetType();
52 if (T != this.lastType)
53 {
54 this.lastType = T;
55
56 PropertyInfo PI = T.GetRuntimeProperty(this.name);
57
58 if (!(PI is null))
59 {
60 this.lastFieldInfo = null;
61
62 if (PI.CanRead && PI.GetMethod.IsPublic)
63 this.lastPropertyInfo = PI;
64 else
65 this.lastPropertyInfo = null;
66 }
67 else
68 {
69 this.lastPropertyInfo = null;
70
71 FieldInfo FI = T.GetRuntimeField(this.name);
72
73 if (!(FI is null) && FI.IsPublic)
74 this.lastFieldInfo = FI;
75 else
76 this.lastFieldInfo = null;
77 }
78 }
79
80 if (!(this.lastPropertyInfo is null))
81 Value = ScriptNode.UnnestPossibleTaskSync(this.lastPropertyInfo.GetValue(Value));
82 else if (!(this.lastFieldInfo is null))
83 Value = ScriptNode.UnnestPossibleTaskSync(this.lastFieldInfo.GetValue(Value));
84 else
85 Value = null;
86
87 return Expression.Encapsulate(Value);
88 }
89 }
90}
Class managing a script expression.
Definition: Expression.cs:39
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:4955
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
static object UnnestPossibleTaskSync(object Result)
Checks if Result is an asynchronous results. If so, blocks the current thread until the result is co...
Definition: ScriptNode.cs:436
Orders elements based on their values.
Definition: ElementOrder.cs:14
int Compare(IElement x, IElement y)
Compares two elements.
Definition: ElementOrder.cs:32
Orders elements based on values of a given property.
int Compare(IElement x, IElement y)
Compares two elements.
PropertyOrder(ScriptNode Node, string Name, int Sign)
Orders elements based on values of a given property.
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33