[英]Automapper mapping Enum to Class
我收到“缺少類型映射配置或不支持的映射”的一般錯誤。 當我嘗試創建此地圖時。 有任何想法嗎?
Mapper.CreateMap<MyEnum, MyClass>().ConvertUsing(c =>
{
MyAttribute attribute = c.GetCustomAttribute<MyEnum, MyAttribute>();
return new MyClass()
{
Id = c.ToString(),
Name = attribute == null ? c.ToString() : attribute.DisplayName
};
});
和...
protected override void Configure()
{
base.Configure();
Mapper.CreateMap<MyEnum, MyClass>()
.ForMember(d => d.Id, opt => opt.MapFrom(s => s.ToString()))
.ForMember(d => d.Name, opt => opt.ResolveUsing<DisplayNameResolver>());
}
private class DisplayNameResolver : ValueResolver<MyEnum, string>
{
protected override string ResolveCore(MyEnum e)
{
MyAttribute attribute = e.GetCustomAttribute<MyEnum, MyAttribute>();
return attribute == null ? e.ToString() : attribute.DisplayName;
}
}
似乎不起作用。
謝謝。
此錯誤的一個症狀是您沒有在應用程序根目錄中調用Configure()。 如果您的配置有問題..我建議您進行單元測試並調用AssertConfigurationIsValid()
的AssertConfigurationIsValid()
- 配置驗證頁
[TestMethod]
public void BaseMapperWorks()
{
//MapperConfig is my static MapperCongfiguration Class
MapperConfig.Configure();
Mapper.AssertConfigurationIsValid();
}
AutoMapper Validatior將為您提供映射錯誤的所有信息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.