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

NewArrayInit() public static method

Creates a NewArrayExpression of the specified type from the provided initializers.
public static NewArrayInit ( Type type, IEnumerable initializers ) : NewArrayExpression
type Type A Type that represents the element type of the array.
initializers IEnumerable The expressions used to create the array elements.
return NewArrayExpression
        public static NewArrayExpression NewArrayInit(Type type, IEnumerable<Expression> initializers)
        {
            ContractUtils.RequiresNotNull(type, nameof(type));
            ContractUtils.RequiresNotNull(initializers, nameof(initializers));
            if (type == typeof(void))
            {
                throw Error.ArgumentCannotBeOfTypeVoid(nameof(type));
            }

            TypeUtils.ValidateType(type, nameof(type));
            if (type.IsByRef)
            {
                throw Error.TypeMustNotBeByRef(nameof(type));
            }

            if (type.IsPointer)
            {
                throw Error.TypeMustNotBePointer(nameof(type));
            }

            ReadOnlyCollection<Expression> initializerList = initializers.ToReadOnly();

            Expression[] newList = null;
            for (int i = 0, n = initializerList.Count; i < n; i++)
            {
                Expression expr = initializerList[i];
                RequiresCanRead(expr, nameof(initializers), i);

                if (!TypeUtils.AreReferenceAssignable(type, expr.Type))
                {
                    if (!TryQuote(type, ref expr))
                    {
                        throw Error.ExpressionTypeCannotInitializeArrayType(expr.Type, type);
                    }
                    if (newList == null)
                    {
                        newList = new Expression[initializerList.Count];
                        for (int j = 0; j < i; j++)
                        {
                            newList[j] = initializerList[j];
                        }
                    }
                }
                if (newList != null)
                {
                    newList[i] = expr;
                }
            }
            if (newList != null)
            {
                initializerList = new TrueReadOnlyCollection<Expression>(newList);
            }

            return NewArrayExpression.Make(ExpressionType.NewArrayInit, type.MakeArrayType(), initializerList);
        }

Same methods

Expression::NewArrayInit ( Type type ) : NewArrayExpression

Usage Example

Ejemplo n.º 1
0
        public void NewArrayInit()
        {
            var expected = LinqExpression.NewArrayInit(typeof(long), LinqExpression.Constant(0L));

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

:s
    a :NewArrayInit ;
    :newArrayType [
        :typeName ""System.Int64"" ;
    ] ;
    :newArrayExpressions (
        [
            :constantValue 0 ;
        ]
    ) ;
.
");
            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::NewArrayInit
Expression