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

ArrayAccess() public static method

Creates an IndexExpression to access an array.
The expression representing the array can be obtained by using the MakeMemberAccess method, or through NewArrayBounds or NewArrayInit.
public static ArrayAccess ( Expression array, IEnumerable indexes ) : IndexExpression
array Expression An expression representing the array to index.
indexes IEnumerable An containing expressions used to index the array.
return IndexExpression
        public static IndexExpression ArrayAccess(Expression array, IEnumerable<Expression> indexes)
        {
            RequiresCanRead(array, nameof(array));

            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();
            }

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

            return new IndexExpression(array, null, indexList);
        }

Same methods

Expression::ArrayAccess ( Expression array ) : IndexExpression

Usage Example

示例#1
0
        public void ArrayAccess()
        {
            var expected = LinqExpression.ArrayAccess(
                LinqExpression.Parameter(
                    typeof(int[])),
                LinqExpression.Parameter(
                    typeof(int)));

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

:s
    :arrayAccessArray [
        :parameterType [
            :typeName ""System.Int32[]"" ;
        ] ;
    ] ;
    :arrayAccessIndexes (
        [
            :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::ArrayAccess
Expression