Suppose we have this class
class ClassA
{
public ClassB[] MyProperty { get; set; }
}
class ClassB
{
public int[] AnotherProperty { get; set; }
}
Now I want to access the value for AnotherProperty
for a given instance of ClassA
and an array of indexes that reflect the two arrays So { 3 , 5 }
e.g. means the fourth element within the top-level (evaluates to MyProperty
) and the sixth element within second level (AnotherProperty
). Thus I´m going to create a Func<ClassA, int[], int>
by the means of an expression-tree:
var instanceArgument = Expression.Parameter(typeof(MyClass), "x");
var indexesArgument = Expression.Parameter(typeof(int[]), "i");
var expr = instanceArgument;
expr = Expression.Property(expr, "MyProperty");
expr = Expression.ArrayIndex(expr, ???);
expr = Expression.Property(expr, "AnotherProperty");
expr = Expression.ArrayIndex(expr, ???);
var f = Expression.Lambda<Func<ClassA, int[], int>>(expr, instanceArgument, indexesArgument);
As you can see I´m unsure on how to provide the indexes to the expression. I know I have to use the indexesArgument
which reflects the indexes passed to our delegate, but how to access the values 3
and 5
?
MyProperty[3].AnotherProperty[5]
. From this string I extract the names of the attributes and the indexes – HimBromBeere Nov 29 '17 at 15:34