public Expression CreateNewExpression(AnonTypeNewTweaks tweaks)
        {
            if (UseBuiltInTypes)
            {
                return(AnonType.CreateNewExpression(MemberExpressions, tweaks));
            }

            var typeArguments = MemberExpressions.Select(i => i.Type).ToArray();
            var type          = DynamicClassBridge.CreateType(typeArguments);

            return(AnonType.CreateNewExpression(type, typeArguments, MemberExpressions, false));
        }
Esempio n. 2
0
        public static Expression CreateNewExpression(IReadOnlyCollection <Expression> expressions, AnonTypeNewTweaks tweaks)
        {
            if (tweaks != null)
            {
                if (!tweaks.AllowEmpty && expressions.Count < 1)
                {
                    return(Expression.Constant(1));
                }

                if (!tweaks.AllowUnusedMembers)
                {
                    expressions = Pad(expressions, SnapSize(expressions.Count));
                }
            }

            var typeArguments = expressions.Select(i => i.Type).ToArray();

            return(CreateNewExpression(Get(typeArguments), typeArguments, expressions, true));
        }
Esempio n. 3
0
        public static Expression CreateNewExpression(ICollection <Expression> expressions, AnonTypeNewTweaks tweaks)
        {
            if (tweaks != null)
            {
                if (!tweaks.AllowEmpty && expressions.Count < 1)
                {
                    return(Expression.Constant(1));
                }

                if (!tweaks.AllowUnusedMembers)
                {
                    var unusedCount = SnapSize(expressions.Count) - expressions.Count;

                    if (unusedCount > 0)
                    {
                        expressions = new List <Expression>(expressions);
                    }

                    while (unusedCount > 0)
                    {
                        expressions.Add(Expression.Constant(false));
                        unusedCount--;
                    }
                }
            }

            var typeArguments = expressions.Select(i => i.Type).ToArray();
            var type          = Get(typeArguments);

            return(Expression.New(
                       type.GetConstructor(typeArguments),
                       expressions,
                       Enumerable.Range(0, typeArguments.Length).Select(i => type.GetField(IndexToField(i)))
                       ));
        }