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;
9
11{
16 {
17 private readonly string name;
18
28 : base(NamedMember.Operand, Operand, Start, Length, Expression)
29 {
30 this.name = NamedMember.Name;
31 }
32
36 public string Name => this.name;
37
44 {
45 IElement Left = this.left.Evaluate(Variables);
46 IElement Right = this.right.Evaluate(Variables);
47
48 return this.Evaluate(Left, Right);
49 }
50
56 public override async Task<IElement> EvaluateAsync(Variables Variables)
57 {
58 if (!this.isAsync)
59 return this.Evaluate(Variables);
60
61 IElement Left = await this.left.EvaluateAsync(Variables);
62 IElement Right = await this.right.EvaluateAsync(Variables);
63
64 return this.Evaluate(Left, Right);
65 }
66
67 private IElement Evaluate(IElement Left, IElement Right)
68 {
69 object LeftValue = Left.AssociatedObjectValue;
70 Type Type = LeftValue.GetType();
71
72 lock (this.synchObject)
73 {
74 if (Type != this.type)
75 {
76 this.type = Type;
77 this.property = Type.GetRuntimeProperty(this.name);
78 if (!(this.property is null))
79 {
80 this.field = null;
81 this.nameIndex = null;
82
83 if (!this.property.CanWrite)
84 throw new ScriptRuntimeException("Property cannot be set: " + this.name, this);
85 else if (!this.property.SetMethod.IsPublic)
86 throw new ScriptRuntimeException("Property not accessible: " + this.name, this);
87 }
88 else
89 {
90 this.field = Type.GetRuntimeField(this.name);
91 if (!(this.field is null))
92 {
93 this.nameIndex = null;
94
95 if (!this.field.IsPublic)
96 throw new ScriptRuntimeException("Field not accessible: " + this.name, this);
97 }
98 else
99 {
100 if (VectorIndex.TryGetIndexProperty(Type, false, true, out this.property, out _))
101 {
102 if (this.nameIndex is null)
103 this.nameIndex = new string[] { this.name };
104 }
105 else
106 this.nameIndex = null;
107 }
108 }
109 }
110
111 if (!(this.property is null))
112 {
113 Type = this.property.PropertyType;
114 if (Type == typeof(object))
115 this.property.SetValue(LeftValue, Right.AssociatedObjectValue, this.nameIndex);
116 else if (Type.GetTypeInfo().IsAssignableFrom(Right.GetType().GetTypeInfo()))
117 this.property.SetValue(LeftValue, Right, this.nameIndex);
118 else
119 this.property.SetValue(LeftValue, Expression.ConvertTo(Right, Type, this), this.nameIndex);
120 }
121 else if (!(this.field is null))
122 {
123 Type = this.field.FieldType;
124 if (!Type.GetTypeInfo().IsAssignableFrom(Right.GetType().GetTypeInfo()))
125 this.field.SetValue(Left, Expression.ConvertTo(Right, Type, this));
126 else
127 this.field.SetValue(Left, Right);
128 }
129 else
130 throw new ScriptRuntimeException("Member '" + this.name + "' not found on type '" + Type.FullName + "'.", this);
131 }
132
133 return Right;
134 }
135
136 private Type type = null;
137 private PropertyInfo property = null;
138 private FieldInfo field = null;
139 private string[] nameIndex = null;
140 private readonly object synchObject = new object();
141
142 }
143}
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
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: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