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)); }
public WhereCollectionExpression(ICollectionExpression <T> source, Expression <Func <T, bool> > predicate, Func <T, bool> predicateCompiled, Action <T, bool> predicateReversed) : base(source, predicate, predicateCompiled) { if (predicateCompiled == null) { var predicateReverseExpression = SetExpressionRewriter.CreateSetter(predicate); if (predicateReverseExpression != null) { predicateReversed = predicateReverseExpression.Compile(); } } PredicateSetter = predicateReversed; }
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)); }
public override INotifyExpression <T> ApplyParameters(IDictionary <string, object> parameters) { if (parameters == null) { throw new ArgumentNullException("parameters"); } var applyParameters = new RepositoryChangeApplyParametersVisitor(parameters); var newBody = applyParameters.Visit(Body); var changeInfos = ChangeInfos; if (applyParameters.Recorders != null) { if (changeInfos == null) { changeInfos = applyParameters.Recorders; } else { changeInfos = new List <IChangeInfo>(changeInfos); changeInfos.AddRange(applyParameters.Recorders); } } if (parameters.Count == Parameters.Count) { var lambda = Expression.Lambda <Func <T> >(newBody); var setter = SetExpressionRewriter.CreateSetter(lambda); if (setter == null) { throw new InvalidOperationException(string.Format("The lambda expression {0} could not be reversed.", newBody)); } if (changeInfos == null) { return(new RepositoryAffectedReversableNotifyValue <T>(Repository, lambda.Compile(), setter.Compile())); } else { return(new RepositoryAffectedDependentReversableNotofyValue <T>(Repository, lambda.Compile(), setter.Compile(), changeInfos)); } } else { return(new RepositoryAffectedReversableNotifyFunc <T>(Repository, newBody, Parameters.Where(p => !parameters.ContainsKey(p.Name)), changeInfos)); } }
public INotifyReversableExpression <T> CreateReversableExpression <T>(Expression expression, IEnumerable <ParameterExpression> parameters, IDictionary <string, object> parameterMappings) { if (parameterMappings != null) { var applyParameters = new ApplyParametersVisitor(parameterMappings); expression = applyParameters.Visit(expression); } if (parameters == null || (parameterMappings == null ? !parameters.Any() : parameters.Count() == parameterMappings.Count)) { var lambda = Expression.Lambda <Func <T> >(expression); var setter = SetExpressionRewriter.CreateSetter(lambda); if (setter == null) { throw new InvalidOperationException(string.Format("The lambda expression {0} could not be reversed.", expression)); } return(new RepositoryAffectedReversableNotifyValue <T>(Repository, lambda.Compile(), setter.Compile())); } else { return(new RepositoryAffectedReversableNotifyFunc <T>(Repository, expression, parameters)); } }
public static Binding <T, TMember> Create <TMember>(Expression <Func <T, TMember> > func, Expression <Func <T, TMember> > member) { var setter = SetExpressionRewriter.CreateSetter(member) ?? throw new InvalidOperationException($"The expression {member.ToString()} could not be inverted."); return(Create(func, setter.Compile())); }