Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
DynamicMemberAssignment.cs
1using System;
2using System.Reflection;
3using System.Threading.Tasks;
9
11{
16 {
27 {
28 }
29
36 {
37 IElement Left = this.left.Evaluate(Variables);
38 IElement Middle = this.middle.Evaluate(Variables);
39 IElement Right = this.right.Evaluate(Variables);
40
41 if (Middle.IsScalar)
42 return this.Evaluate(Left, Middle, Right, Variables);
43 else
44 {
45 foreach (IElement MiddleElement in Middle.ChildElements)
46 this.Evaluate(Left, MiddleElement, Right, Variables);
47
48 return Right;
49 }
50 }
51
57 public override async Task<IElement> EvaluateAsync(Variables Variables)
58 {
59 if (!this.isAsync)
60 return this.Evaluate(Variables);
61
62 IElement Left = await this.left.EvaluateAsync(Variables);
63 IElement Middle = await this.middle.EvaluateAsync(Variables);
64 IElement Right = await this.right.EvaluateAsync(Variables);
65
66 if (Middle.IsScalar)
67 return this.Evaluate(Left, Middle, Right, Variables);
68 else
69 {
70 foreach (IElement MiddleElement in Middle.ChildElements)
71 this.Evaluate(Left, MiddleElement, Right, Variables);
72
73 return Right;
74 }
75 }
76
86 {
87 if (!(Middle.AssociatedObjectValue is string Name))
88 throw new ScriptRuntimeException("Member names must be strings.", this);
89
90 object LeftValue = Left.AssociatedObjectValue;
91 Type Type = LeftValue.GetType();
92
93 PropertyInfo Property = Type.GetRuntimeProperty(Name);
94 if (!(Property is null))
95 {
96 if (!Property.CanWrite)
97 throw new ScriptRuntimeException("Property cannot be set: " + Name, this);
98 else if (!Property.SetMethod.IsPublic)
99 throw new ScriptRuntimeException("Property not accessible: " + Name, this);
100 else
101 {
102 Type = Property.PropertyType;
103 if (!Type.GetTypeInfo().IsAssignableFrom(Right.GetType().GetTypeInfo()))
104 Property.SetValue(LeftValue, Expression.ConvertTo(Right, Type, this), null);
105 else
106 Property.SetValue(LeftValue, Right, null);
107 }
108 }
109 else
110 {
111 FieldInfo Field = Type.GetRuntimeField(Name);
112 if (!(Field is null))
113 {
114 if (!Field.IsPublic)
115 throw new ScriptRuntimeException("Field not accessible: " + Name, this);
116 else
117 {
118 Type = Field.FieldType;
119 if (!Type.GetTypeInfo().IsAssignableFrom(Right.GetType().GetTypeInfo()))
120 Field.SetValue(Left, Expression.ConvertTo(Right, Type, this));
121 else
122 Field.SetValue(Left, Right);
123 }
124 }
125 else
126 {
127 if (VectorIndex.TryGetIndexProperty(Type, false, true, out Property, out _))
128 {
129 Type = Property.PropertyType;
130 if (Type == typeof(object))
131 Property.SetValue(LeftValue, Right.AssociatedObjectValue, new string[] { Name });
132 else if (Type.GetTypeInfo().IsAssignableFrom(Right.GetType().GetTypeInfo()))
133 Property.SetValue(LeftValue, Right, new string[] { Name });
134 else
135 Property.SetValue(LeftValue, Expression.ConvertTo(Right, Type, this), new string[] { Name });
136 }
137 else
138 throw new ScriptRuntimeException("Member '" + Name + "' not found on type '" + Type.FullName + "'.", this);
139 }
140 }
141
142 return Right;
143 }
144
145 }
146}
Class managing a script expression.
Definition: Expression.cs:39
static object ConvertTo(IElement Value, Type DesiredType, ScriptNode Node)
Tries to conevert an element value to a desired type.
Definition: Expression.cs:5127
ScriptNode RightOperand
Right operand.
ScriptNode LeftOperand
Left operand.
ScriptNode right
Right operand.
ScriptNode left
Left operand.
bool isAsync
If subtree is asynchroneous.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
abstract IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection. This method should be ...
virtual Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection. This method should be ...
Definition: ScriptNode.cs:158
Base class for all ternary operators.
ScriptNode middle
Middle operand.
DynamicMemberAssignment(DynamicMember DynamicMember, ScriptNode Operand, int Start, int Length, Expression Expression)
Dynamic member Assignment operator.
override IElement Evaluate(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
IElement Evaluate(IElement Left, IElement Middle, IElement Right, Variables Variables)
Performs scalar dynamic member assignment.
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
static bool TryGetIndexProperty(Type T, bool ForReading, bool ForWriting, out PropertyInfo PropertyInfo, out ParameterInfo[] Parameters)
Tries to get a one-dimensional index property of a Type.
Definition: VectorIndex.cs:133
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20
object AssociatedObjectValue
Associated object value.
Definition: IElement.cs:33
ICollection< IElement > ChildElements
An enumeration of child elements. If the element is a scalar, this property will return null.
Definition: IElement.cs:49
bool IsScalar
If the element represents a scalar value.
Definition: IElement.cs:41