Suppose I have a type like this:
class Context
{
SomeType[] Items { get; set; }
}
I want to be able to access specific Items
elements using expression trees. Suppose I need an element at index 0
. I can do it like below, where everything works as expected:
var type = typeof (Context);
var param = Expression.Parameter(typeof (object));
var ctxExpr= Expression.Convert(param, context);
var proInfo = type.GetProperty("Items");
Expression.ArrayIndex(Expression.Property(ctxExpr, proInfo), Expression.Constant(0));
If I change the context type to contain .NET provided List<SomeType>
, instead of array
, i.e.
class Context
{
List<SomeType> Items { get; set; }
}
the same expression results in following error:
System.ArgumentException: Argument must be array at System.Linq.Expressions.Expression.ArrayIndex(Expression array, Expression index)
My question is, how to write respective expression which can access an item under appropriate index of List<>
, or better - of any collection declaring indexer? E.g. is there is some way to detect, and convert such a collection to an array
of appropriate types using expression trees?