0
一个属性我使用AutoMapper
到我的MVC ViewModels
映射到DTO
,然后再返回,并想知道是否有可能利用从AutoMapper
一个TypeMap
仅经由属性映射值名称?使用Automapper.TypeMap映射的名字
我的情况是这样的:
- 我通过Automapper映射
ViewModel
到DTO
。 - 我通过WCF调用发送
DTO
,验证失败。 - 我得到一个
FaultException
回来,失败的DTO
字段的名称。 - 我需要设置
ModelState
字典,其中ViewModel
属性的名称失败,并显示错误消息。这使我可以突出显示屏幕上的错误字段。
所以我的问题在于,能够映射失败,回到ViewModel
属性DTO
属性的string
名称。我想这样做:
var typemap = Mapper.FindTypeMapFor(typeof(DTO), typeof(Model))
string erroredPropertyName = "Prop1"; // Really extracted from my Fault
// This is the bit that doesn't exist, and I need help with......
// string destination = typemap.GetDestinationFor(erroredPropertyName);
ModelState.Add(destination, errorMessage);
这是可能的吗?如果需要,我不介意使用Reflection
,但如果可以的话,我宁愿不使用它。