zoukankan      html  css  js  c++  java
  • Construction构造函数

    AutoMapper can map to destination constructors based on source members:

    public class Source {
        public int Value { get; set; }
    }
    public class SourceDto {
        public SourceDto(int value) {
            _value = value;
        }
        private int _value;
        public int Value {
            get { return _value; }
        }
    }
    Mapper.Initialize(cfg => cfg.CreateMap<Source, SourceDto>());
    

    If the destination constructor parameter names don't match, you can modify them at config time:

    public class Source {
        public int Value { get; set; }
    }
    public class SourceDto {
        public SourceDto(int valueParamSomeOtherName) {
            _value = valueParamSomeOtherName;
        }
        private int _value;
        public int Value {
            get { return _value; }
        }
    }
    Mapper.Initialize(cfg => 
      cfg.CreateMap<Source, SourceDto>()
        .ForCtorParam("valueParamSomeOtherName", opt => opt.MapFrom(src => src.Value))
    );
    

    This works for both LINQ projections and in-memory mapping.

    You can also disable constructor mapping :

    Mapper.Initialize(cfg => cfg.DisableConstructorMapping());
  • 相关阅读:
    pat1111-1120
    pat1101-1110
    pat1091-1100
    pat1081-1090
    pat1071-1080
    pat1061-1070
    2017华为软件精英挑战赛总结
    pat1051-1060
    【转】WPF中PasswordBox控件的Password属性的数据绑定
    Python学习-41.Python中的断言
  • 原文地址:https://www.cnblogs.com/Leman/p/5774323.html
Copyright © 2011-2022 走看看