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

ArrayIndex() public static method

Creates a BinaryExpression that represents applying an array index operator to an array of rank one.
public static ArrayIndex ( Expression array, Expression index ) : BinaryExpression
array Expression An to set the property equal to.
index Expression An to set the property equal to.
return BinaryExpression
        public static BinaryExpression ArrayIndex(Expression array, Expression index)
        {
            RequiresCanRead(array, nameof(array));
            RequiresCanRead(index, nameof(index));
            if (index.Type != typeof(int))
            {
                throw Error.ArgumentMustBeArrayIndexType(nameof(index));
            }

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

            return new SimpleBinaryExpression(ExpressionType.ArrayIndex, array, index, arrayType.GetElementType());
        }

Same methods

Expression::ArrayIndex ( Expression array ) : MethodCallExpression
Expression::ArrayIndex ( Expression array, IEnumerable indexes ) : 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