简体   繁体   English

自动映射器返回null

[英]Automapper returns null

I've the abstract class AbsProduct 我有抽象类AbsProduct

public abstract class AbsProduct
{
    [Key]
    public int ID { get; set; }
    public int Price { get; set; }
    public int Category { get; set; }
    public string Name { get; set; }

    public abstract double Accept(IProductVisitor visitor);

}

and the ProductDTO : ProductDTO

public class ProductDTO
{
    public int ID { get; set; }
    public int Price { get; set; }
    public int Category { get; set; }
    public string Name { get; set; }
}

My configuration is 我的配置是

AutoMapper.Mapper.Initialize(config =>
{
    config.CreateMap<AbsProduct, ProductDTO>();
    config.CreateMap<ProductDTO, AbsProduct>();
});

The problem is when I try to map ProductDTO to AbsProduct : 问题是当我尝试将ProductDTO映射到AbsProduct

var product = AutoMapper.Mapper.Map<ProductDTO, AbsProduct>(productDTO);

AutoMapper is returning null , but the source( productDTO ) isn't null . AutoMapper返回null ,但是source( productDTO )不为null

You can't instantiate an abstract class. 您不能实例化一个abstract类。

Try create a type that derives from AbsProduct and use that type instead of it. 尝试创建一个从AbsProduct派生的类型,并使用该类型代替它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM