IndexExpression Klasa

Definicja

Reprezentuje indeksowanie właściwości lub tablicy.

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
Dziedziczenie
IndexExpression
Implementuje

Przykłady

W poniższym przykładzie kodu pokazano, jak utworzyć obiekt IndexExpression typu i użyć go do zmiany wartości elementu tablicy przy użyciu ArrayAccess metody .

// 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

Właściwości

Arguments

Pobiera argumenty, które będą używane do indeksowania właściwości lub tablicy.

CanReduce

Wskazuje, że węzeł można zmniejszyć do prostszego węzła. Jeśli zwraca wartość true, funkcja Reduce() może zostać wywołana w celu utworzenia skróconej postaci.

(Odziedziczone po Expression)
Indexer

PropertyInfo Pobiera właściwość dla właściwości, jeśli wyrażenie reprezentuje indeksowaną właściwość, zwraca wartość null w przeciwnym razie.

NodeType

Zwraca typ węzła tego Expressionelementu .

Object

Obiekt do indeksowania.

Type

Pobiera statyczny typ wyrażenia, które Expression reprezentuje.

Metody

Accept(ExpressionVisitor)

Wysyła do określonej metody wizyty dla tego typu węzła. Na przykład MethodCallExpression wywołuje element VisitMethodCall(MethodCallExpression).

(Odziedziczone po Expression)
Equals(Object)

Określa, czy dany obiekt jest taki sam, jak bieżący obiekt.

(Odziedziczone po Object)
GetHashCode()

Służy jako domyślna funkcja skrótu.

(Odziedziczone po Object)
GetType()

Type Pobiera bieżące wystąpienie.

(Odziedziczone po Object)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
Reduce()

Zmniejsza ten węzeł do prostszego wyrażenia. Jeśli funkcja CanReduce zwróci wartość true, powinna zwrócić prawidłowe wyrażenie. Ta metoda może zwrócić inny węzeł, który musi zostać zmniejszony.

(Odziedziczone po Expression)
ReduceAndCheck()

Zmniejsza ten węzeł do prostszego wyrażenia. Jeśli funkcja CanReduce zwróci wartość true, powinna zwrócić prawidłowe wyrażenie. Ta metoda może zwrócić inny węzeł, który musi zostać zmniejszony.

(Odziedziczone po Expression)
ReduceExtensions()

Zmniejsza wyrażenie do znanego typu węzła (który nie jest węzłem rozszerzenia) lub po prostu zwraca wyrażenie, jeśli jest już znanym typem.

(Odziedziczone po Expression)
ToString()

Zwraca tekstową reprezentację obiektu Expression.

(Odziedziczone po Expression)
Update(Expression, IEnumerable<Expression>)

Tworzy nowe wyrażenie, które jest podobne do tego, ale przy użyciu dostarczonych elementów podrzędnych. Jeśli wszystkie elementy podrzędne są takie same, zwróci to wyrażenie.

VisitChildren(ExpressionVisitor)

Zmniejsza węzeł, a następnie wywołuje delegata gościa w wyrażeniu zredukowanym. Metoda zgłasza wyjątek, jeśli węzeł nie jest reducible.

(Odziedziczone po Expression)

Jawne implementacje interfejsu

IArgumentProvider.ArgumentCount

Zwraca liczbę argumentów do węzła drzewa wyrażeń. Nie należy używać tego elementu członkowskiego. Jest ona publiczna tylko ze względu na refaktoryzację zestawu i jest używana wewnętrznie do optymalizacji wydajności.

IArgumentProvider.GetArgument(Int32)

Zwraca argument w indeksie, zgłaszając, czy indeks jest poza granicami. Nie należy używać tego elementu członkowskiego. Jest ona publiczna tylko ze względu na refaktoryzację zestawu i jest używana wewnętrznie do optymalizacji wydajności.

Dotyczy