IndexExpression 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表索引屬性或陣列。
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 類型的物件,並利用該方法改變陣列元素 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
屬性
| 名稱 | Description |
|---|---|
| Arguments |
取得用來索引屬性或陣列的參數。 |
| CanReduce |
表示該節點可以簡化為更簡單的節點。 若此結果為真,則可呼叫 Reduce() 以產生約簡形式。 (繼承來源 Expression) |
| Indexer |
若該表達式代表索引屬性,則取得 PropertyInfo 屬性,否則回傳 null。 |
| NodeType |
回傳此 Expression節點的類型。 |
| Object |
一個可以索引的物件。 |
| Type |
取得此 Expression 表達式的靜態型態。 |
方法
| 名稱 | Description |
|---|---|
| Accept(ExpressionVisitor) |
針對此節點類型的特定訪問方法進行派遣。 例如,稱為 MethodCallExpressionVisitMethodCall(MethodCallExpression)。 (繼承來源 Expression) |
| Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
| GetHashCode() |
做為預設哈希函式。 (繼承來源 Object) |
| GetType() |
取得目前實例的 Type。 (繼承來源 Object) |
| MemberwiseClone() |
建立目前 Object的淺層複本。 (繼承來源 Object) |
| Reduce() |
將此節點簡化為更簡單的表達式。 如果 CanReduce 回傳為真,則應回傳一個有效的表達式。 此方法可返回另一個必須被約簡的節點。 (繼承來源 Expression) |
| ReduceAndCheck() |
將此節點簡化為更簡單的表達式。 如果 CanReduce 回傳為真,則應回傳一個有效的表達式。 此方法可返回另一個必須被約簡的節點。 (繼承來源 Expression) |
| ReduceExtensions() |
將表達式簡化為已知節點型別(非擴充節點),或僅回傳已是已知型別的表達式。 (繼承來源 Expression) |
| ToString() |
回傳 的文字表示 Expression。 (繼承來源 Expression) |
| Update(Expression, IEnumerable<Expression>) |
會建立一個新的表達式,類似這個,但使用提供的子節點。 如果所有子節點相同,則會回傳這個表達式。 |
| VisitChildren(ExpressionVisitor) |
將節點簡化,然後呼叫訪客代理處理簡化表達式。 若節點不可約,方法會拋出例外。 (繼承來源 Expression) |
明確介面實作
| 名稱 | Description |
|---|---|
| IArgumentProvider.ArgumentCount |
回傳給表達式樹節點的參數數量。 你不應該使用這個會員。 它之所以公開,是因為組合語言重構,並且用於內部效能優化。 |
| IArgumentProvider.GetArgument(Int32) |
回傳 在 索引 的參數,若索引超出範圍則拋棄。 你不應該使用這個會員。 它之所以公開,是因為組合語言重構,並且用於內部效能優化。 |