I'm writing code that evaluates .NET Expression
trees. I'm trying to create a C# 4 test to exercise my handling of an ExpressionType.Index
, but I can't figure out how to create that type of expression through a LambdaExpression
. No matter what I try, the expression comes out as an ExpressionType.Call
or ExpressionType.ArrayIndex
. For example:
IList<int> myList = new ObservableCollection<int> { 3, 56, 8 };
Expression<Func<int>> myExpression = () => myList[3];
// myExpression.Body.NodeType == ExpressionType.Call
myList = new int[] { 3, 56, 8 };
myExpression = () => myList[3];
// myExpression.Body.NodeType == ExpressionType.Call
int[] myArray = new int[] { 3, 56, 8 };
myExpression = () => myArray[3];
// myExpression.Body.NodeType == ExpressionType.ArrayIndex
List<int> myNonInterfaceList = new List<int> { 3, 7, 4, 2 };
myExpression = () => myNonInterfaceList[3];
// myExpression.Body.NodeType == ExpressionType.Call
What is an IndexExpression
, and can one be created through an inline LambdaExpression
in C# 4?