问题描述
我正在写计算.NET 表达式
树木代码。我试图创建一个C#4的测试行使我的 ExpressionType.Index
处理,但我无法弄清楚如何通过创建一个类型的表达式 LambdaExpression
。不管我怎么努力,表达出来作为 ExpressionType.Call
或 ExpressionType.ArrayIndex
。例如:
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
什么是 IndexExpression
,并且可以在一个通过内联 LambdaExpression
在C#4?
What is an IndexExpression
, and can one be created through an inline LambdaExpression
in C# 4?
推荐答案
这是 IndexExpression
正是你所期望的(即数组访问或索引属性)。它是从DLR移植了许多新的表达式类型之一。在C#4.0编译器,但是,使用相同的表达式类型作为其以前的版本,所以它不会使用 IndexExpression
的任何地方。其他语言可以这样做,如果他们的设计师希望它。
An IndexExpression
is exactly what you expect (i.e., array access or indexer property). It's one of the many new expression types that was ported over from the DLR. The C# 4.0 compiler, however, uses the same expression types as its previous version, so it won't use IndexExpression
anywhere. Other languages may do so if their designers wish it.
要创建一个 IndexExpression
编程,使用静态 ArrayAccess接口()
, MakeIndex()
或属性()
方法在表达
类。
To create an IndexExpression
programmatically, use the static ArrayAccess()
,MakeIndex()
, or Property()
methods on the Expression
class.
这篇关于怎样才能创建ExpressionType.Index的一个的NodeType .NET表达?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!