System.Linq.Expressions.Expression.ArrayIndex C# (CSharp) Method

ArrayIndex() public static method

Creates a MethodCallExpression that represents applying an array index operator to an array of rank more than one.
/// or is null. /// .Type does not represent an array type.-or-The rank of .Type does not match the number of elements in .-or-The property of one or more elements of does not represent the type.
public static ArrayIndex ( Expression array, IEnumerable indexes ) : MethodCallExpression
array Expression An to set the property equal to.
indexes IEnumerable An that contains objects to use to populate the collection.
return MethodCallExpression
        public static MethodCallExpression ArrayIndex(Expression array, IEnumerable<Expression> indexes)
        {
            RequiresCanRead(array, nameof(array));
            ContractUtils.RequiresNotNull(indexes, nameof(indexes));

            Type arrayType = array.Type;
            if (!arrayType.IsArray)
            {
                throw Error.ArgumentMustBeArray(nameof(array));
            }

            ReadOnlyCollection<Expression> indexList = indexes.ToReadOnly();
            if (arrayType.GetArrayRank() != indexList.Count)
            {
                throw Error.IncorrectNumberOfIndexes();
            }

            for (int i = 0, n = indexList.Count; i < n; i++)
            {
                Expression e = indexList[i];

                RequiresCanRead(e, nameof(indexes), i);
                if (e.Type != typeof(int))
                {
                    throw Error.ArgumentMustBeArrayIndexType(nameof(indexes), i);
                }
            }

            MethodInfo mi = array.Type.GetMethod("Get", BindingFlags.Public | BindingFlags.Instance);
            return Call(array, mi, indexList);
        }

Same methods

Expression::ArrayIndex ( Expression array, Expression index ) : BinaryExpression
Expression::ArrayIndex ( Expression array ) : MethodCallExpression

Usage Example

Ejemplo n.º 1
0
        public void ArrayIndex_index()
        {
            var expected = LinqExpression.ArrayIndex(
                LinqExpression.Parameter(
                    typeof(int[])),
                LinqExpression.Parameter(
                    typeof(int)));

            using var g = new GraphEngine.Graph();
            g.LoadFromString(@"
@prefix : <http://example.com/> .

:s
    :arrayIndexArray [
        :parameterType [
            :typeName ""System.Int32[]"" ;
        ] ;
    ] ;
    :arrayIndexIndex [
        :parameterType [
            :typeName ""System.Int32"" ;
        ] ;
    ] ;
.
");
            var s = g.GetUriNode(":s");

            var actual = Expression.Parse(s).LinqExpression;

            Console.WriteLine(actual.GetDebugView());

            actual.Should().Be(expected);
        }
All Usage Examples Of System.Linq.Expressions.Expression::ArrayIndex
Expression