Example #1
0
        public static ObjectInitLambda ParseNewExpression <Tin, Tout>(Expression <Func <Tin, Tout> > newExpression, Tin input = null)
            where Tin : class where Tout : class
        {
            var body = newExpression.Body as NewExpression;

            if (body == null)
            {
                throw new ArgumentException("Expression must be a new anonymous initialiazer");
            }

            var r = new List <InitMemberAssignment>();

            var i = 0;

            foreach (var member in body.Members)
            {
                var argumentExpression = body.Arguments[i++];
                var inputParameter     = argumentExpression.GetFirstParameterOfType <Tin>();

                var lambda = Expression.Lambda(argumentExpression, newExpression.Parameters);

                var initMemberAssignment = new InitMemberAssignment
                {
                    Property              = member as PropertyInfo,
                    ValueExpression       = argumentExpression,
                    ValueLambdaExpression = lambda,
                };

                if (argumentExpression.NodeType == ExpressionType.Constant && argumentExpression is ConstantExpression constantExpression)
                {
                    initMemberAssignment.Type          = InitMemberAssignmentType.Constant;
                    initMemberAssignment.ConstantValue = constantExpression.Value;
                }
                else if (argumentExpression.NodeType == ExpressionType.MemberAccess &&
                         argumentExpression is MemberExpression memberExpression &&
                         memberExpression.Expression == inputParameter)
                {
                    initMemberAssignment.MappedProperty = memberExpression.Member as PropertyInfo;
                    initMemberAssignment.Type           = InitMemberAssignmentType.Property;
                }

                if (input != null && initMemberAssignment.Type != InitMemberAssignmentType.Constant)
                {
                    initMemberAssignment.EffectiveValue = initMemberAssignment.CompiledLambdaExpression.DynamicInvoke(input);
                }

                r.Add(initMemberAssignment);
            }

            return(new ObjectInitLambda(newExpression, r));
        }
Example #2
0
        private static List <InitMemberAssignment> ParseInitExpressionInternal <Tin, Tout>(Expression <Func <Tin, Tout> > initExpression, Tin input = null)
            where Tin : class where Tout : class
        {
            var memberInitExpression = GetBody(initExpression);
            var inputParameter       = initExpression.Parameters.First();

            var r = new List <InitMemberAssignment>();

            foreach (MemberBinding binding in memberInitExpression.Bindings)
            {
                if (!(binding is MemberAssignment memberAssignment))
                {
                    throw new ArgumentException("The init expression MemberBinding must only by type MemberAssignment.");
                }

                var valueExpression = memberAssignment.Expression;
                var lambda          = Expression.Lambda(valueExpression, inputParameter);

                var initMemberAssignment = new InitMemberAssignment
                {
                    Property              = binding.Member as PropertyInfo,
                    ValueExpression       = valueExpression,
                    ValueLambdaExpression = lambda,
                };

                if (valueExpression.NodeType == ExpressionType.Constant && valueExpression is ConstantExpression constantExpression)
                {
                    initMemberAssignment.Type          = InitMemberAssignmentType.Constant;
                    initMemberAssignment.ConstantValue = constantExpression.Value;
                }
                else if (valueExpression.NodeType == ExpressionType.MemberAccess &&
                         valueExpression is MemberExpression memberExpression &&
                         memberExpression.Expression == inputParameter)
                {
                    initMemberAssignment.MappedProperty = memberExpression.Member as PropertyInfo;
                    initMemberAssignment.Type           = InitMemberAssignmentType.Property;
                }

                if (input != null && initMemberAssignment.Type != InitMemberAssignmentType.Constant)
                {
                    initMemberAssignment.EffectiveValue = initMemberAssignment.CompiledLambdaExpression.DynamicInvoke(input);
                }

                r.Add(initMemberAssignment);
            }

            return(r);
        }
Example #3
0
        private static List <InitMemberAssignment> ParseInitExpression <T>(MemberInitExpression memberInitExpression)
            where T : class
        {
            var r = new List <InitMemberAssignment>();

            foreach (MemberBinding binding in memberInitExpression.Bindings)
            {
                if (!(binding is MemberAssignment memberAssignment))
                {
                    throw new ArgumentException("The init expression MemberBinding must only by type MemberAssignment.", "memberInitExpression");
                }

                var valueExpression = memberAssignment.Expression;
                var lambda          = Expression.Lambda(valueExpression);

                var initMemberAssignment = new InitMemberAssignment
                {
                    Property              = binding.Member as PropertyInfo,
                    ValueExpression       = valueExpression,
                    ValueLambdaExpression = lambda,
                };

                if (valueExpression.NodeType == ExpressionType.Constant && valueExpression is ConstantExpression constantExpression)
                {
                    initMemberAssignment.Type           = InitMemberAssignmentType.Constant;
                    initMemberAssignment.ConstantValue  = constantExpression.Value;
                    initMemberAssignment.EffectiveValue = initMemberAssignment.ConstantValue;
                }
                else
                {
                    initMemberAssignment.EffectiveValue = initMemberAssignment.CompiledLambdaExpression.DynamicInvoke();
                }

                r.Add(initMemberAssignment);
            }

            return(r);
        }