예제 #1
0
        public static LambdaExpression CreateSetter(LambdaExpression getter)
        {
            if (getter == null)
            {
                throw new ArgumentNullException("getter");
            }
            var valueParameter = Expression.Parameter(getter.ReturnType);
            var visitor        = new SetExpressionRewriter(valueParameter);
            var body           = visitor.Visit(getter.Body);

            if (body == null)
            {
                return(null);
            }
            var parameters = new ParameterExpression[getter.Parameters.Count + 1];

            getter.Parameters.CopyTo(parameters, 0);
            parameters[parameters.Length - 1] = valueParameter;
            var typeParameters = new Type[getter.Parameters.Count + 1];

            for (int i = 0; i < getter.Parameters.Count; i++)
            {
                typeParameters[i] = getter.Parameters[i].Type;
            }
            typeParameters[typeParameters.Length - 1] = getter.ReturnType;
            var delegateType = actionTypes[getter.Parameters.Count].MakeGenericType(typeParameters);

            return(Expression.Lambda(delegateType, body, parameters));
        }
예제 #2
0
        public static Expression <Action <T, TValue> > CreateSetter <T, TValue>(Expression <Func <T, TValue> > getter)
        {
            if (getter == null)
            {
                throw new ArgumentNullException("getter");
            }
            var p       = Expression.Parameter(typeof(TValue));
            var visitor = new SetExpressionRewriter(p);
            var body    = visitor.Visit(getter.Body);

            if (body == null)
            {
                return(null);
            }
            return(Expression.Lambda <Action <T, TValue> >(body, getter.Parameters[0], p));
        }