Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
MethodLambda.cs
1using System;
2using System.Reflection;
3using System.Threading.Tasks;
6
8{
13 {
14 private readonly object obj;
15 private readonly MethodInfo method;
16 private readonly ParameterInfo[] parameters;
17 private readonly int nrParameters;
18 private string[] argumentNames = null;
19 private ArgumentType[] argumentTypes = null;
20
26 public MethodLambda(object Object, MethodInfo Method)
27 {
28 this.obj = Object;
29 this.method = Method;
30 this.parameters = Method.GetParameters();
31 this.nrParameters = this.parameters.Length;
32 }
33
37 public int NrArguments => this.nrParameters;
38
42 public string[] ArgumentNames
43 {
44 get
45 {
46 if (this.argumentNames is null)
47 {
48 string[] Names = new string[this.nrParameters];
49 int i;
50
51 for (i = 0; i < this.nrParameters; i++)
52 Names[i] = this.parameters[i].Name;
53
54 this.argumentNames = Names;
55 }
56
57 return this.argumentNames;
58 }
59 }
60
65 {
66 get
67 {
68 if (this.argumentTypes is null)
69 {
70 ArgumentType[] Types = new ArgumentType[this.nrParameters];
71 int i;
72
73 for (i = 0; i < this.nrParameters; i++)
74 {
75 Type T = this.parameters[i].GetType();
76 if (T.IsArray || T is IVector)
77 Types[i] = ArgumentType.Vector;
78 else if (T is IMatrix)
79 Types[i] = ArgumentType.Matrix;
80 else if (T is Abstraction.Sets.ISet)
81 Types[i] = ArgumentType.Set;
82 else
83 Types[i] = ArgumentType.Normal;
84 }
85
86 this.argumentTypes = Types;
87 }
88
89 return this.argumentTypes;
90 }
91 }
92
100 {
101 return this.EvaluateAsync(Arguments, Variables).Result;
102 }
103
108 public bool IsAsynchronous => true;
109
116 public async Task<IElement> EvaluateAsync(IElement[] Arguments, Variables Variables)
117 {
118 int i, c = Arguments.Length;
119 object[] P = new object[c];
120 object Obj;
121 Type T, T0;
122
123 for (i = 0; i < c; i++)
124 {
125 Obj = Arguments[i].AssociatedObjectValue;
126 if (!(Obj is null))
127 {
128 T = Obj.GetType();
129 T0 = this.parameters[i].ParameterType;
130 if (T != T0)
131 {
132 if (Arguments[i].TryConvertTo(T0, out object Obj0))
133 Obj = Obj0;
134 }
135 }
136
137 P[i] = Obj;
138 }
139
140 object Result = this.method.Invoke(this.obj, P);
141 Result = await ScriptNode.WaitPossibleTask(Result);
142 return Expression.Encapsulate(Result);
143 }
144
146 public override string ToString()
147 {
148 return Operators.LambdaDefinition.ToString(this);
149 }
150 }
151}
Class managing a script expression.
Definition: Expression.cs:39
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:4955
Lambda expression executing object methods.
Definition: MethodLambda.cs:13
bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
IElement Evaluate(IElement[] Arguments, Variables Variables)
Evaluates the lambda expression.
Definition: MethodLambda.cs:99
MethodLambda(object Object, MethodInfo Method)
Lambda expression executing object methods.
Definition: MethodLambda.cs:26
ArgumentType[] ArgumentTypes
Argument types.
Definition: MethodLambda.cs:65
async Task< IElement > EvaluateAsync(IElement[] Arguments, Variables Variables)
Evaluates the lambda expression.
Extract the names of an enumeration type or an enumeration object.
Definition: Names.cs:15
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
static async Task< object > WaitPossibleTask(object Result)
Waits for any asynchronous process to terminate.
Definition: ScriptNode.cs:417
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
Basic interface for matrices.
Definition: IMatrix.cs:9
Basic interface for vectors.
Definition: IVector.cs:9
Base interface for lambda expressions.
ArgumentType
Type of parameter used in a function definition or a lambda definition.
Definition: IFunction.cs:9