C#更改Linq表达式的返回值

一些背景:这只是我第一次玩Expression对象并尝试简化我在其中一个应用程序中使用的东西.我已经是一名C#开发人员超过10年了,但直到现在才有理由跳到表达式.

我正在尝试生成一个扩展方法来修改表达式的返回值.更具体地说,我正在尝试添加一个在创建AutoMapper地图时使用的扩展方法.

示例地图:

map.CreateMap<Widget, WidgetModel>()
   .ForMember(x => x.Count, opts => opts.MapFrom(src => src.Count + 1));

我想要做的是创建一个扩展方法,所以我不必在任何地方都这样做,只需将其写成:

map.CreateMap<Widget, WidgetModel>()
   .Increment(x => x.Count, src => src.Count);

并且让Increment()为我做1.

我已经环顾四周,意识到ExpressionVisitor可能就是我所需要的,但我不确定从那里去哪里?

question看起来很相似,但我无法将这项工作转化为我的用例.

最佳答案 您可以使用
Expression.Increment而不是1.

或者我们可以使它非常通用.

像这样的东西:

void Main()
{
    var mapperConfiguraiton = 
        new MapperConfiguration(cfg => 
            cfg.CreateMap<Widget, WidgetModel>()
               .Increment(x => x.CountD, src => src.Count)
               .ToUpper(x => x.Name, src=>src.Name));

    var widget = new Widget {Count = 3, Name="Jimmy"};
    var mapper = mapperConfiguraiton.CreateMapper();

    var model = mapper.Map<WidgetModel>(widget);
}

public class Widget {
    public int Count {get; set;}
    public string Name {get;set;}
}

public class WidgetModel {
    public int Count {get; set;}
    public string Name {get;set;}
}

public static class MapperExtensions {
    public static IMappingExpression<TSource, TDestination> Increment<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression, 
        Expression<Func<TDestination, int>> destinationMember, 
        Expression<Func<TSource, int>> sourceMember) 
    {
        return expression.CustomAction(destinationMember, sourceMember, s => s + 1);
     }

    public static IMappingExpression<TSource, TDestination> ToUpper<TSource, TDestination>(this IMappingExpression<TSource, TDestination> expression, 
        Expression<Func<TDestination, string>> destinationMember, 
        Expression<Func<TSource, string>> sourceMember)
    {
        return expression.CustomAction(destinationMember, sourceMember, s => s.ToUpper());
    }


    public static IMappingExpression<TSource, TDestination> CustomAction<TSource, TDestination, TDestinationMember, TSourceMember>(
        this IMappingExpression<TSource, TDestination> expression, 
        Expression<Func<TDestination, TDestinationMember>> destinationMember, 
        Expression<Func<TSource, TSourceMember>> sourceMember, 
        Expression<Func<TSourceMember, TDestinationMember>> transform)
    {
       var sourceMemberExpression = (MemberExpression)sourceMember.Body;        
       var sourceParameter = Expression.Parameter(typeof(TSource));

       var expr = Expression.Invoke(transform, 
                                Expression
                                    .MakeMemberAccess(sourceParameter, sourceMemberExpression.Member));

        var lambda = (Expression<Func<TSource,TSourceMember>>)
            Expression.Lambda(expr, sourceParameter);

        var newExpression = expression.ForMember(
             destinationMember, 
             opts => opts.MapFrom(lambda));

        return newExpression;
    }
}

输出:

widget.Count = 3
widget.Name = "Jimmy"

model.Count = 4
model.Name = "JIMMY"
点赞