IndexExpression 类

定义

表示对一个属性或数组进行索引。

public ref class IndexExpression sealed : System::Linq::Expressions::Expression, System::Linq::Expressions::IArgumentProvider
public ref class IndexExpression sealed : System::Linq::Expressions::Expression
public sealed class IndexExpression : System.Linq.Expressions.Expression, System.Linq.Expressions.IArgumentProvider
public sealed class IndexExpression : System.Linq.Expressions.Expression
type IndexExpression = class
    inherit Expression
    interface IArgumentProvider
type IndexExpression = class
    inherit Expression
Public NotInheritable Class IndexExpression
Inherits Expression
Implements IArgumentProvider
Public NotInheritable Class IndexExpression
Inherits Expression
继承
IndexExpression
实现

示例

下面的代码示例演示如何创建 类型的 对象 IndexExpression ,并使用它通过 方法更改数组元素 ArrayAccess 的值。

// Add the following directive to your file:
// using System.Linq.Expressions;

// This parameter expression represents a variable that will hold the array.
ParameterExpression arrayExpr = Expression.Parameter(typeof(int[]), "Array");

// This parameter expression represents an array index.
ParameterExpression indexExpr = Expression.Parameter(typeof(int), "Index");

// This parameter represents the value that will be added to a corresponding array element.
ParameterExpression valueExpr = Expression.Parameter(typeof(int), "Value");

// This expression represents an array access operation.
// It can be used for assigning to, or reading from, an array element.
Expression arrayAccessExpr = Expression.ArrayAccess(
    arrayExpr,
    indexExpr
);

// This lambda expression assigns a value provided to it to a specified array element.
// The array, the index of the array element, and the value to be added to the element
// are parameters of the lambda expression.
Expression<Func<int[], int, int, int>> lambdaExpr = Expression.Lambda<Func<int[], int, int, int>>(
    Expression.Assign(arrayAccessExpr, Expression.Add(arrayAccessExpr, valueExpr)),
    arrayExpr,
    indexExpr,
    valueExpr
);

// Print out expressions.
Console.WriteLine("Array Access Expression:");
Console.WriteLine(arrayAccessExpr.ToString());

Console.WriteLine("Lambda Expression:");
Console.WriteLine(lambdaExpr.ToString());

Console.WriteLine("The result of executing the lambda expression:");

// The following statement first creates an expression tree,
// then compiles it, and then executes it.
// Parameters passed to the Invoke method are passed to the lambda expression.
Console.WriteLine(lambdaExpr.Compile().Invoke(new int[] { 10, 20, 30 }, 0, 5));

// This code example produces the following output:
//
// Array Access Expression:
// Array[Index]

// Lambda Expression:
// (Array, Index, Value) => (Array[Index] = (Array[Index] + Value))

// The result of executing the lambda expression:
// 15
' Add the following directive to your file:
' Imports System.Linq.Expressions  

' This parameter expression represents a variable that will hold the array.
Dim arrayExpr As ParameterExpression = Expression.Parameter(GetType(Integer()), "Array")

' This parameter expression represents an array index.
' For multidimensional arrays, you can define several indexes. 
Dim indexExpr As ParameterExpression = Expression.Parameter(GetType(Integer), "Index")

' This parameter represents the value that will be added to a corresponding array element.
Dim valueExpr As ParameterExpression = Expression.Parameter(GetType(Integer), "Value")

' This expression represents an array access operation.
' It can be used for assigning to, or reading from, an array element.
Dim arrayAccessExpr As Expression = Expression.ArrayAccess(
    arrayExpr,
    indexExpr
)

' This lambda expression assigns a value provided to it to a specified array element.
' The array, the index of the array element, and the value to be added to the element
' are parameters of the lambda expression.
Dim lambdaExpr As Expression(Of Func(Of Integer(), Integer, Integer, Integer)) =
    Expression.Lambda(Of Func(Of Integer(), Integer, Integer, Integer))(
        Expression.Assign(arrayAccessExpr, Expression.Add(arrayAccessExpr, valueExpr)),
    arrayExpr,
    indexExpr,
    valueExpr
  )

' Print expressions.
Console.WriteLine("Array Access Expression:")
Console.WriteLine(arrayAccessExpr.ToString())

Console.WriteLine("Lambda Expression:")
Console.WriteLine(lambdaExpr.ToString())

Console.WriteLine("The result of executing the lambda expression:")

' The following statement first creates an expression tree,
' then compiles it, and then executes it.
' Parameters passed to the Invoke method are passed to the lambda expression.
Console.WriteLine(lambdaExpr.Compile().Invoke(New Integer() {10, 20, 30}, 0, 5))

' This code example produces the following output:
'
' Array Access Expression:
' Array[Index]

' Lambda Expression:
' (Array, Index, Value) => (Array[Index] = (Array[Index] + Value))

' The result of executing the lambda expression:
' 15

属性

Arguments

获取将用于为属性或数组编制索引的参数。

CanReduce

指示可将节点简化为更简单的节点。 如果返回 true,则可以调用 Reduce() 以生成简化形式。

(继承自 Expression)
Indexer

如果表达式表示索引属性,则获取属性的 PropertyInfo,否则返回 null。

NodeType

返回此 Expression 的节点类型。

Object

要编制索引的对象。

Type

获取此 Expression 表示的表达式的静态类型。

方法

Accept(ExpressionVisitor)

调度到此节点类型的特定 Visit 方法。 例如,MethodCallExpression 调用 VisitMethodCall(MethodCallExpression)

(继承自 Expression)
Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
Reduce()

将此节点简化为更简单的表达式。 如果 CanReduce 返回 true,则它应返回有效的表达式。 此方法可以返回本身必须简化的另一个节点。

(继承自 Expression)
ReduceAndCheck()

将此节点简化为更简单的表达式。 如果 CanReduce 返回 true,则它应返回有效的表达式。 此方法可以返回本身必须简化的另一个节点。

(继承自 Expression)
ReduceExtensions()

将表达式简化为已知节点类型(即非 Extension 节点)或仅在此类型为已知类型时返回表达式。

(继承自 Expression)
ToString()

返回 Expression 的的文本化表示形式。

(继承自 Expression)
Update(Expression, IEnumerable<Expression>)

创建一个新的表达式,它类似于此表达式,但使用所提供的子级。 如果所有子级均相同,它将返回此表达式。

VisitChildren(ExpressionVisitor)

简化节点,然后对简化的表达式调用访问者委托。 该方法在节点不可简化时引发异常。

(继承自 Expression)

显式接口实现

IArgumentProvider.ArgumentCount

返回表达式树节点的参数数目。 不应使用此成员。 由于程序集重构,只能为公共的;并且在内部用于性能优化。

IArgumentProvider.GetArgument(Int32)

索引时返回参数,当索引超出界限时引发。 不应使用此成员。 由于程序集重构,只能为公共的;并且在内部用于性能优化。

适用于