AutoMapper with prefix

后端 未结 2 1507
陌清茗
陌清茗 2021-01-17 10:42

I\'m trying to use Automapper to map to objects, the issue is one of the objects I\'m trying to map has a prefix \'Cust_\' in front of all its properties and one doesn\'t. I

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-17 11:09

    Mapper.Initialize(cfg =>
    {
       cfg.RecognizeDestinationPrefixes("Cust_");
       cfg.CreateMap();
    });
    
    A a = new A() {FirstName = "Cliff", LastName = "Mayson"};
    B b = Mapper.Map(a);
    
    //b.Cust_FirstName is "Cliff"
    //b.Cust_LastName is "Mayson"
    

    Or alternatively:

    Mapper.Configuration.RecognizeDestinationPrefixes("Cust_");
    Mapper.CreateMap();
    ...
    B b = Mapper.Map(a);
    ...
    

提交回复
热议问题