Automapper返回null(Automapper returns null)

我有抽象类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); }

和ProductDTO :

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

我的配置是

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

问题是当我尝试将ProductDTO映射到AbsProduct :

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

AutoMapper返回null ,但源( productDTO )不为null 。

I've the abstract class 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:

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:

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

AutoMapper is returning null, but the source(productDTO) isn't null.

最满意答案

您无法实例化abstract类。

尝试创建一个派生自AbsProduct的类型,并使用该类型代替它。

You can't instantiate an abstract class.

Try create a type that derives from AbsProduct and use that type instead of it.

更多推荐