使用自动映射器映射多级嵌套对象
Mapping multi-level nestingobjects using automapper
我有 dto 对象:
public class SwivelInfoToViewDTO
{
public ResponseValue 值{ get;放; }
public List<ResponseDocument> Documents { get; set; }
public string Status { get; set; }
}
public class ResponseValue
{
public string SerialNumber { get; set; }
public string ProductCode { get; set; }
public string ProductName { get; set; }
}
public class ResponseDocument
{
public string Language { get; set; }
public int DocumentTypeId { get; set; }
public string DocumentType { get; set; }
}
我有我的灵魂class:
public class SwivelInformationResponse
{
public ResponseValue Value { get; set; }
public string Status { get; set; }
}
public class ResponseValue
{
[JsonProperty("serial_number")]
public string SerialNumber { get; set; }
[JsonProperty("product_code")]
public string ProductCode { get; set; }
public List<ResponseDocument> Documents { get; set; }
}
public class ResponseDocument
{
[JsonProperty("language")]
public string Language { get; set; }
[JsonProperty("document_type_id")]
public int DocumentTypeId { get; set; }
[JsonProperty("document_type")]
public string DocumentType { get; set; }
}
我使用自动映射器,我的个人资料如下所示:
public MappingProfile()
{
CreateMap<SwivelInformationResponse, SwivelInfoToViewDTO>()
.ForMember(x => x.Value, s => s.MapFrom(src => src.Value))
.ForMember(x => x.Documents, s => s.MapFrom(src => src.Value.Documents));
}
但不知何故我得到了一个错误:
错误映射类型。
目的地会员:
值
如何正确绑定?
您忘记映射 ResponseValue 和 ResponseDocument 类。它不一样 类 所以你也需要映射它们。
public MappingProfile()
{
CreateMap<SourceNamespace.ResponseValue, DtoNamespace.ResponseValue>();
CreateMap<SourceNamespace.ResponseDocument, DtoNamespace.ResponseDocument>();
}
我有 dto 对象:
public class SwivelInfoToViewDTO { public ResponseValue 值{ get;放; }
public List<ResponseDocument> Documents { get; set; }
public string Status { get; set; }
}
public class ResponseValue
{
public string SerialNumber { get; set; }
public string ProductCode { get; set; }
public string ProductName { get; set; }
}
public class ResponseDocument
{
public string Language { get; set; }
public int DocumentTypeId { get; set; }
public string DocumentType { get; set; }
}
我有我的灵魂class:
public class SwivelInformationResponse
{
public ResponseValue Value { get; set; }
public string Status { get; set; }
}
public class ResponseValue
{
[JsonProperty("serial_number")]
public string SerialNumber { get; set; }
[JsonProperty("product_code")]
public string ProductCode { get; set; }
public List<ResponseDocument> Documents { get; set; }
}
public class ResponseDocument
{
[JsonProperty("language")]
public string Language { get; set; }
[JsonProperty("document_type_id")]
public int DocumentTypeId { get; set; }
[JsonProperty("document_type")]
public string DocumentType { get; set; }
}
我使用自动映射器,我的个人资料如下所示:
public MappingProfile()
{
CreateMap<SwivelInformationResponse, SwivelInfoToViewDTO>()
.ForMember(x => x.Value, s => s.MapFrom(src => src.Value))
.ForMember(x => x.Documents, s => s.MapFrom(src => src.Value.Documents));
}
但不知何故我得到了一个错误: 错误映射类型。 目的地会员: 值
如何正确绑定?
您忘记映射 ResponseValue 和 ResponseDocument 类。它不一样 类 所以你也需要映射它们。
public MappingProfile()
{
CreateMap<SourceNamespace.ResponseValue, DtoNamespace.ResponseValue>();
CreateMap<SourceNamespace.ResponseDocument, DtoNamespace.ResponseDocument>();
}