这是我的原始模型:
public class MyClass
{
public string Name{get;set;}
public double Latitude{get;set;}
public double Longitude{get;set;}
public string Street{get;set;}
public string City{get;set;}
public string State{get;set;}
public string Zip{get;set;}
}
我想将它映射到此用于JSON序列化目的:
public class MyNewClass
{
public string Name{get;set;}
public Location{get;set;}
}
public class Location
{
public string Street{get;set;}
public string City{get;set;}
public string State{get;set;}
public string Zip{get;set;}
public Coordinates Coordinates{get;set;}
}
public class Coordinates
{
public double Latitude{get;set;}
public double Longitude{get;set;}
}
我似乎无法找到配置映射的正确方法。
答案 0 :(得分:1)
我终于弄清楚了。每个对象都需要显式映射。
Mapper.CreateMap<MyClass, Coordinates>();
Mapper.CreateMap<MyClass, Location>().ForMember(dest => dest.Coordinates, opt => opt.MapFrom(src => src));
Mapper.CreateMap<MyClass, MyNewClass>().ForMember(dest => dest.Location, opt => opt.MapFrom(src => src));