我正在编写评估 .NETExpression
树的代码。我正在尝试创建一个 C# 4 测试来练习我对 的处理ExpressionType.Index
,但我不知道如何通过LambdaExpression
. 无论我尝试什么,表达式都会以ExpressionType.Call
or形式出现ExpressionType.ArrayIndex
。例如:
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
什么是IndexExpression
, 可以通过LambdaExpression
C# 4 中的内联创建吗?