4

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);
    }
}
Pelle
  • 2,755
  • 7
  • 42
  • 49

1 Answers1

8

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;
            }));
Volker Holz
  • 106
  • 1
  • 8