AutoMapper with prefix

后端 未结 2 1485
陌清茗
陌清茗 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:13

    The documentation has an article on Recognizing pre/postfixes

    Sometimes your source/destination properties will have common pre/postfixes that cause you to have to do a bunch of custom member mappings because the names don't match up. To address this, you can recognize pre/postfixes:

    public class Source {
        public int frmValue { get; set; }
        public int frmValue2 { get; set; }
    }
    public class Dest {
        public int Value { get; set; }
        public int Value2 { get; set; }
    }
    Mapper.Initialize(cfg => {
        cfg.RecognizePrefix("frm");
        cfg.CreateMap();
    });
    

    Mapper.AssertConfigurationIsValid(); By default AutoMapper recognizes the prefix "Get", if you need to clear the prefix:

    Mapper.Initialize(cfg => {
        cfg.ClearPrefixes();
        cfg.RecognizePrefixes("tmp");
    });
    

提交回复
热议问题