嗨,我正在尝试将 AutoMapper 添加到我的应用程序中,但我似乎在 th eintegrationg 中遇到了一些问题。这是我到目前为止所拥有的。
为了不创建对 Automapper 的直接依赖,我为其最基本的功能创建了一个简单的映射:
public class AutoMapper : IAutoMapper
{
public void CreateMap<TFrom, TTo>()
{
Mapper.CreateMap<TFrom, TTo>();
}
public TTo Map<TFrom, TTo>(TFrom data)
{
return Mapper.Map<TFrom, TTo>(data);
}
}
我创建了一个配置文件:
public class AutoMapperConfig
{
private readonly IAutoMapper mapper;
public AutoMapperConfig(IAutoMapper mapper)
{
this.mapper = mapper;
}
public void RegisterMappings()
{
mapper.CreateMap<ProductDTO , ProductDataContract>();
}
}
并在我的 Global.Asax 中添加了一个调用:
new AutoMapperConfig(new AutoMapper()).RegisterMappings();
我有这两个对象,我想创建一个映射:
public class ProductDTO
{
public int ProductId { get; set; }
public int CategoryId { get; set; }
public int SubcategoryId { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Description { get; set; }
public string ImagePath { get; set; }
public int NumberOfProducts { get; set; }
}
public class ProductDataContract
{
public int ProductId { get; set; }
public int CategoryId { get; set; }
public int SubcategoryId { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Description { get; set; }
public string ImagePath { get; set; }
public int NumberOfProducts { get; set; }
}
在我的代码中,我放置了这一行用于测试目的:
var products = productCatalogService.GetProducts(); ProductDTO ceva = products.FirstOrDefault(); var productsDataContract = mapper.Map(ceva);
问题是,当我运行我的应用程序时,我在尝试 CreateMap 时立即在 Automapper 中遇到异常。这是类型初始化异常消息:
此平台不支持此类型 IDictionaryFactory
我究竟做错了什么?