C# 如何使用AutoMapper创建新字段?
我想在C#中映射对象时创建新字段并替换其他字段,如下所示C# 如何使用AutoMapper创建新字段?,c#,automapper-6,C#,Automapper 6,我想在C#中映射对象时创建新字段并替换其他字段,如下所示 public class one { public int a {get; set;} public int b {get; set;} public int c {get; set;} } public class two { public int sum {get; set;} //sum = a + b +c ; } Mapper.Initialize(cfg => { cfg.Creat
public class one
{
public int a {get; set;}
public int b {get; set;}
public int c {get; set;}
}
public class two
{
public int sum {get; set;} //sum = a + b +c ;
}
Mapper.Initialize(cfg =>
{
cfg.CreateMap<one, two>();//?????mapping sum = a+b+c;
});
公共一级
{
公共int a{get;set;}
公共int b{get;set;}
公共int c{get;set;}
}
公共二班
{
公共整数和{get;set;}//sum=a+b+c;
}
Mapper.Initialize(cfg=>
{
cfg.CreateMap();/;
});
请,有什么想法吗?使用
(PS您的类和属性应以大写字母开头)
公共一级
{
公共int A{get;set;}
公共int B{get;set;}
公共int C{get;set;}
}
公共二班
{
公共整数和{get;set;}//Sum=a+b+c;
}
cfg.CreateMap()
.ForMember(dest=>dest.Sum,m=>m.MapFrom(src=>src.A+src.B+src.C));
让automapper执行这种逻辑是一种非常可疑的方法,神奇的突变从来都不是一件好事。
public class One
{
public int A {get; set;}
public int B {get; set;}
public int C {get; set;}
}
public class Two
{
public int Sum {get; set;} //sum = a + b +c ;
}
cfg.CreateMap<One, Two>()
.ForMember(dest => dest.Sum, m => m.MapFrom(src => src.A + src.B + src.C));