ListInit() public static method

Creates a ListInitExpression that uses a method named "Add" to add elements to a collection.
public static ListInit ( NewExpression newExpression ) : ListInitExpression
newExpression NewExpression A to set the property equal to.
return ListInitExpression
        public void ListInit()
        {
            var expression =
                LinqExpression.ListInit(
                    LinqExpression.New(
                        typeof(List <long>)),
                    LinqExpression.ElementInit(
                        typeof(List <long>).GetMethod("Add"),
                        LinqExpression.Constant(0L)));

            ShouldRoundrip(expression);
        }
Ejemplo n.º 2
0
        internal virtual Expression VisitListInit(ListInitExpression init)
        {
            var n            = VisitNew(init.NewExpression);
            var initializers = VisitElementInitializerList(init.Initializers);

            if (n != init.NewExpression ||
                initializers != init.Initializers)
            {
                return(Expression.ListInit(n, initializers));
            }
            return(init);
        }
Ejemplo n.º 3
0
 /// <inheritdoc />
 protected override Expression VisitListInit(ListInitExpression init) => Expression.ListInit(VisitNew(init.NewExpression), VisitElementInitializerList(init.Initializers));
Ejemplo n.º 4
0
        public static MemberListBinding ListBind(MethodInfo propertyAccessor, ElementInit[] initializers) => Expression.ListBind(propertyAccessor, initializers); // NB: Used for properties.

        public static ListInitExpression ListInit(NewExpression newExpression, ElementInit[] initializers) => Expression.ListInit(newExpression, initializers);
Ejemplo n.º 5
0
        Exp ParseUnit(List <Token> expr)
        {
            Token tok;

            if (expr.Count == 0)
            {
                return(NoneExp);
            }

            else if (expr.Count == 1)
            {
                tok = expr[0];

                switch (tok.Type)
                {
                case TokenType.Identifier:
                    // keywords
                    switch (tok.Value)
                    {
                    case "None":
                        return(NoneExp);

                    case "True":
                        return(TrueExp);

                    case "False":
                        return(FalseExp);
                    }
                    // user identifiers
                    if (Local != null && Local.TryGetValue(tok.Value, out Exp value))
                    {
                        return(value);
                    }
                    else
                    {
                        return(GlobalAccess(tok.Value));
                    }

                case TokenType.Number:
                    if (int.TryParse(tok.Value, out int i))
                    {
                        return(Exp.Constant(new Int(i), Any));
                    }

                    else if (double.TryParse(tok.Value, out double d))
                    {
                        return(Exp.Constant(new Float(d), Any));
                    }

                    break;

                case TokenType.String:
                    return(Exp.Constant(new String(tok.Value), Any));

                case TokenType.Parenthesis:
                {
                    var w = Split(tok.Subset, TokenType.Comma);

                    if (w.Count == 1)         // parenthesis
                    {
                        return(Parse(w[0]));
                    }

                    // tuple

                    if (w.Count == 0)
                    {
                        return(Exp.New(typeof(Tuple)));
                    }
                    else
                    {
                        return(Exp.New(typeof(Tuple).GetConstructor(new[] { typeof(Object[]) }),
                                       Exp.NewArrayInit(typeof(Object), w.Select(Parse))));
                    }
                }

                case TokenType.Brackets:
                {
                    var w = Split(tok.Subset, TokenType.Comma);

                    if (w.Count == 0)
                    {
                        return(Exp.New(typeof(List)));
                    }
                    else
                    {
                        return(Exp.New(typeof(List).GetConstructor(new[] { typeof(Object[]) }),
                                       Exp.NewArrayInit(typeof(Object), w.Select(Parse))));
                    }
                }

                case TokenType.Braces:
                {
                    var w = Split(tok.Subset, TokenType.Comma);

                    if (Contains(w[0], TokenType.Colon))
                    {
                        // dict
                        if (w.Count == 0)
                        {
                            return(Exp.New(typeof(Dict)));
                        }
                        else
                        {
                            var items = new List <ElementInit>();
                            var adder = typeof(Dictionary <Object, Object>).GetMethod("Add");

                            foreach (var v in w)
                            {
                                var row   = Split(v, TokenType.Colon);
                                var left  = Parse(row[0]);
                                var right = Parse(row[1]);
                                items.Add(Exp.ElementInit(adder, left, right));
                            }

                            return(Exp.New(typeof(Dict).GetConstructor(new[] { typeof(Dictionary <Object, Object>) }),
                                           Exp.ListInit(Exp.New(typeof(Dictionary <Object, Object>)), items)));
                        }
                    }
                    else
                    {
                        // set
                        if (w.Count == 0)
                        {
                            return(Exp.New(typeof(Set)));
                        }
                        else
                        {
                            return(Exp.New(typeof(Set).GetConstructor(new[] { typeof(Object[]) }),
                                           Exp.NewArrayInit(typeof(Object), w.Select(Parse))));
                        }
                    }
                }
                }
            }
            else // length > 1
            {
                tok = expr[0]; // starts

                if (tok.Type == TokenType.Operator)
                {
                    expr.RemoveAt(0);
                    Exp obj = ParseUnit(expr);

                    switch (tok.op)
                    {
                    case Op.Invert:
                        return(Exp.Call(obj, typeof(Object).GetMethod("__invert__")));

                    case Op.Add:
                        return(Exp.Call(obj, typeof(Object).GetMethod("__pos__")));

                    case Op.Subtract:
                        return(Exp.Call(obj, typeof(Object).GetMethod("__neg__")));

                    case Op.Not:
                        return(Exp.Call(obj, typeof(Object).GetMethod("__not__")));

                    default:
                        throw new Exception("invalid unary operator");
                    }
                }

                tok = expr[^ 1]; // ends