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
,但如果可以的話,我寧願不使用它。