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)

分派給這個節點類型的特定造訪方法。 例如,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)

傳回位於索引的引數,如果索引超出範圍則擲回引數。 您不應該使用這個成員。 它只有在組件重構時為公用,且使用於內部以取得效能最佳化。

適用於