2

我有 2 类DataMainData需要映射

public class Data
{
    public Field<string> Results { get; set; }
    public Field<int> A { get; set; }
    public Field<int> B { get; set; }
}

public class Field<T>
{
 public string Label { get; set; }
 public T Value { get; set; }  
}

public class MainData{
 public string result {get; set; }
 public int a {get; set; }
 public int b {get; set; }
}

我尝试使用 automapper 将Data映射到MainData,如下所示:

Mapper.CreateMap(Data, MainData))
.ForMember(dest => dest.result, conf => conf.MapFrom(src => src.results.Value))
.ForMember(dest => dest.a, conf => conf.MapFrom(src => src.A.Value))
.ForMember(dest => dest.b, conf => conf.MapFrom(src => src.B.Value))

var destination = Mapper.Map<Data, MainData>(source);

它永远不会将映射值映射到目标变量。我得到的只是新的 MainData 对象。

4

0 回答 0