2017-04-02 27 views
0

我有抽象類AbsProductAutomapper返回null

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>(); 
}); 

問題是,當我嘗試映射ProductDTOAbsProduct

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

AutoMapper在返回null,但源(productDTO)不是null

回答

1

您無法實例化abstract類。

嘗試創建一個從AbsProduct派生的類型,並使用該類型代替它。