如何使用 AutoMapper 创建新字段?

How can I create new fields with AutoMapper?

我想在 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.CreateMap<one, two>();//?????mapping sum = a+b+c;
});

拜托,有什么想法吗?

使用 Inline Mapping;

(PS 您的 类 和属性应以大写字母开头)

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));

DotNetFiddle Example