다음을 통해 공유


Expression.ArrayIndex Method (Expression, IEnumerable<Expression>)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Creates a MethodCallExpression that represents applying an array index operator to an array of rank more than one.

Namespace:  System.Linq.Expressions
Assembly:  System.Core (in System.Core.dll)

Syntax

'Declaration
Public Shared Function ArrayIndex ( _
    array As Expression, _
    indexes As IEnumerable(Of Expression) _
) As MethodCallExpression
public static MethodCallExpression ArrayIndex(
    Expression array,
    IEnumerable<Expression> indexes
)

Parameters

Return Value

Type: System.Linq.Expressions.MethodCallExpression
A MethodCallExpression that has the NodeType property equal to Call and the Object and Arguments properties set to the specified values.

Exceptions

Exception Condition
ArgumentNullException

array or indexes is nulla null reference (Nothing in Visual Basic).

ArgumentException

array.Type does not represent an array type.

-or-

The rank of array.Type does not match the number of elements in indexes.

-or-

The Type property of one or more elements of indexes does not represent the Int32 type.

Remarks

Each element of indexes must have Type equal to Int32. The Type property of array must represent an array type whose rank matches the number of elements in indexes.

If the rank of array.Type is 1, this method returns a BinaryExpression. The Left property is set to array and the Right property is set to the single element of indexes. The Type property of the BinaryExpression represents the element type of array.Type.

If the rank of array.Type is more than one, this method returns a MethodCallExpression. The Method property is set to the MethodInfo that describes the public instance method Get on the type represented by the Type property of array.

Examples

The following example demonstrates how to use the ArrayIndex(Expression, array<Expression[]) method to create a MethodCallExpression that represents indexing into a two-dimensional array.

Dim gradeArray(,) As String = _
    {{"chemistry", "history", "mathematics"}, {"78", "61", "82"}}

Dim arrayExpression As System.Linq.Expressions.Expression = _
    System.Linq.Expressions.Expression.Constant(gradeArray)

' Create a MethodCallExpression that represents indexing
' into the two-dimensional array 'gradeArray' at (0, 2).
' Executing the expression would return "mathematics".
Dim methodCallExpression As System.Linq.Expressions.MethodCallExpression = _
    System.Linq.Expressions.Expression.ArrayIndex( _
        arrayExpression, _
        System.Linq.Expressions.Expression.Constant(0), _
        System.Linq.Expressions.Expression.Constant(2))

outputBlock.Text &= methodCallExpression.ToString() & vbCrLf

' This code produces the following output:
'
' value(System.String[,]).Get(0, 2)
string[,] gradeArray = { { "chemistry", "history", "mathematics" }, { "78", "61", "82" } };

System.Linq.Expressions.Expression arrayExpression =
    System.Linq.Expressions.Expression.Constant(gradeArray);

// Create a MethodCallExpression that represents indexing
// into the two-dimensional array 'gradeArray' at (0, 2).
// Executing the expression would return "mathematics".
System.Linq.Expressions.MethodCallExpression methodCallExpression =
    System.Linq.Expressions.Expression.ArrayIndex(
        arrayExpression,
        System.Linq.Expressions.Expression.Constant(0),
        System.Linq.Expressions.Expression.Constant(2));

outputBlock.Text += methodCallExpression.ToString() + "\n";

// This code produces the following output:
//
// value(System.String[,]).Get(0, 2)

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.