我有几个类:
public class Customer
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Product Product { get; set; }
}
public class Product
{
public string Name { get; set; }
}
public class Customer_
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Article Article { get; set; }
}
public class Article
{
public string ArticleName { get; set; }
}
并且一个实例:
Customer[] Customer = new Customer[]
{
new Customer()
{
FirstName = "FirstName1",
LastName = "LastName1",
Product = new Product()
{
Name = "Product1"
}
},
new Customer()
{
FirstName = "FirstName2",
LastName = "LastName2",
Product = new Product()
{
Name = "Product2"
}
}
};
我希望将此实例映射到一个名为
Customer_
的对象,其中Customer.Product.Name
= Customer_.Article.ArticleName
。我使用Automapper:
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Product, Article>()
.ForMember(dest => dest.ArticleName, opt => opt.MapFrom(src => src.Name));
});
IMapper mapper = config.CreateMapper();
var desti = mapper.Map<Customer[], Customer_[]>(Customer);
但是我有一个例外:
Missing type map configuration or unsupported mapping.
Mapping types:
Customer -> Customer_
Xml_Test.Customer -> Xml_Test.Customer_
Destination path:
Customer_[][0]
Source value:
Xml_Test.Customer
有什么问题吗?
cfg.CreateMap<Customer, Customer_>()
。 - Ilya Chumakov