admin管理员组文章数量:1602101
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());
转载于:https://wwwblogs/Leman/p/5774323.html
本文标签: 函数construction
版权声明:本文标题:Construction构造函数 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1728397016a1157129.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论