Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
NamedMemberAssignment.cs
1using System;
2using System.Reflection;
3using System.Threading.Tasks;
10
12{
17 {
18 private readonly string name;
19
29 : base(NamedMember.Operand, Operand, Start, Length, Expression)
30 {
31 this.name = NamedMember.Name;
32 }
33
37 public string Name => this.name;
38
45 {
46 IElement Left = this.left.Evaluate(Variables);
47 IElement Right = this.right.Evaluate(Variables);
48
49 return this.Evaluate(Left, Right);
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 Right = await this.right.EvaluateAsync(Variables);
64
65 return this.Evaluate(Left, Right);
66 }
67
69 {
70 object LeftValue = Left.AssociatedObjectValue;
71 Type Type = LeftValue.GetType();
72
73 lock (this.synchObject)
74 {
75 if (Type != this.type)
76 {
77 this.type = Type;
78 this.property = Type.GetRuntimeProperty(this.name);
79 if (!(this.property is null))
80 {
81 this.field = null;
82 this.nameIndex = null;
83
84 if (!this.property.CanWrite)
85 throw new ScriptRuntimeException("Property cannot be set: " + this.name, this);
86 else if (!this.property.SetMethod.IsPublic)
87 throw new ScriptRuntimeException("Property not accessible: " + this.name, this);
88 }
89 else
90 {
91 this.field = Type.GetRuntimeField(this.name);
92 if (!(this.field is null))
93 {
94 this.nameIndex = null;
95
96 if (!this.field.IsPublic)
97 throw new ScriptRuntimeException("Field not accessible: " + this.name, this);
98 }
99 else
100 {
101 if (VectorIndex.TryGetIndexProperty(Type, false, true, out this.property,
102 out ParameterInfo[] IndexArguments) &&
103 (IndexArguments?.Length ?? 0) == 1)
104 {
105 if (this.nameIndex is null)
106 {
107 if (IndexArguments[0].ParameterType == typeof(string))
108 this.nameIndex = new string[] { this.name };
109 else
110 this.nameIndex = new object[] { Expression.ConvertTo(this.name, IndexArguments[0].ParameterType, this) };
111 }
112 }
113 else
114 this.nameIndex = null;
115 }
116 }
117 }
118
119 if (!(this.property is null))
120 {
121 Type = this.property.PropertyType;
122 if (Type == typeof(object))
123 this.property.SetValue(LeftValue, Right.AssociatedObjectValue, this.nameIndex);
124 else if (Type.IsAssignableFrom(Right.GetType().GetTypeInfo()))
125 this.property.SetValue(LeftValue, Right, this.nameIndex);
126 else
127 this.property.SetValue(LeftValue, Expression.ConvertTo(Right, Type, this), this.nameIndex);
128 }
129 else if (!(this.field is null))
130 {
131 Type = this.field.FieldType;
132 if (!Type.IsAssignableFrom(Right.GetType().GetTypeInfo()))
133 this.field.SetValue(Left, Expression.ConvertTo(Right, Type, this));
134 else
135 this.field.SetValue(Left, Right);
136 }
137 else
138 throw new ScriptRuntimeException("Member '" + this.name + "' not found on type '" + Type.FullName + "'.", this);
139 }
140
141 return Right;
142 }
143
144 private Type type = null;
145 private PropertyInfo property = null;
146 private FieldInfo field = null;
147 private object[] nameIndex = null;
148 private readonly object synchObject = new object();
149
150 }
151}
Class managing a script expression.
Definition: Expression.cs:41
static object ConvertTo(IElement Value, Type DesiredType, ScriptNode Node)
Tries to conevert an element value to a desired type.
Definition: Expression.cs:5385
Base class for all binary operators.
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
override async Task< IElement > EvaluateAsync(Variables Variables)
Evaluates the node, using the variables provided in the Variables collection.
NamedMemberAssignment(NamedMember NamedMember, ScriptNode Operand, int Start, int Length, Expression Expression)
Named member Assignment operator.
override IElement Evaluate(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:138
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:21