JavaEar 专注于收集分享传播有价值的技术资料

Automapper(5.1.1)ForAllMembers - 获取当前属性的名称 (Automapper (5.1.1) ForAllMembers - Get name of the current property)

Here is my problem, in Condition I want to get to the name of the current property being evaluated. I believe you could do this in earlier versions of Automapper. Any suggestions?

[TestFixture]
public class SandBox
{
    public class MySource
    {
        public string Name { get; set; }
        public int Count { get; set; }
    }

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

    public class SourceProfile : Profile
    {
        public SourceProfile()
        {
            this.CreateMap<MySource, MyDestination>()
                .ForAllMembers(x => x.Condition((source, destination, arg3, arg4, resolutionContext) =>
                {
                    // this will run twice (once for every property)
                    // but how can I find out, what the current property is?

                    return true;
                }));
        }
    }

    public SandBox()
    {
        Mapper.Initialize(x =>
        {
            x.AddProfile(new SourceProfile());
        });
    }

    [Test]
    public void Run()
    {
        var s = new MySource { Name = "X", Count = 42 };
        var r = Mapper.Map<MyDestination>(s);
        Assert.AreEqual(s.Name, r.Name);
    }
}

1个回答

    最佳答案

  1. 英文原文

    Try the following:

    this.CreateMap<MySource, MyDestination>()
                .ForAllMembers(x => x.Condition((source, destination, arg3, arg4, resolutionContext) =>
                {
                    // this will run twice (once for every property)
                    // but how can I find out, what the current property is?
    
                    Debug.WriteLine($"Mapping to {destination.GetType().Name}.{x.DestinationMember.Name}");
    
                    return true;
                }));
    

    中文翻译

    尝试以下方法:

      this.CreateMap&lt; MySource,MyDestination&gt;()
                .ForAllMembers(x =&gt; x.Condition((source,destination,arg3,arg4,resolutionContext)=&gt;
                {
                    //这将运行两次(每个属性一次)
                    //但是我如何才能知道当前的财产是什么?
    
                    Debug.WriteLine($" 映射到{destination.GetType()。Name}。{x.DestinationMember.Name}" );
    
                    返回true;
                }));
     

    Try the following:

    this.CreateMap<MySource, MyDestination>()
                .ForAllMembers(x => x.Condition((source, destination, arg3, arg4, resolutionContext) =>
                {
                    // this will run twice (once for every property)
                    // but how can I find out, what the current property is?
    
                    Debug.WriteLine($"Mapping to {destination.GetType().Name}.{x.DestinationMember.Name}");
    
                    return true;
                }));
    

    尝试以下方法:

      this.CreateMap&lt; MySource,MyDestination&gt;()
                .ForAllMembers(x =&gt; x.Condition((source,destination,arg3,arg4,resolutionContext)=&gt;
                {
                    //这将运行两次(每个属性一次)
                    //但是我如何才能知道当前的财产是什么?
    
                    Debug.WriteLine($" 映射到{destination.GetType()。Name}。{x.DestinationMember.Name}" );
    
                    返回true;
                }));