Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用AutoMapper创建新字段?_C#_Automapper 6 - Fatal编程技术网

C# 如何使用AutoMapper创建新字段?

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

我想在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;
});
公共一级
{
公共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));