我制定了一个解决方案,但代码有点乱。要在复杂映射中获取目标属性的路径,然后我可以将其放入 ModelState 中以查找错误,我可以使用以下代码:
public string GetDestinationPath(Type source, Type destination, string propertyName)
{
return GetDestinationPath(source, destination, propertyName, string.Empty);
}
public string GetDestinationPath(Type source, Type destination, string propertyName, string path)
{
var typeMap = Mapper.FindTypeMapFor(source, destination);
if (typeMap == null)
return null;
var maps = typeMap.GetPropertyMaps();
var exactMatch = maps.FirstOrDefault(m => m.SourceMember != null && m.SourceMember.Name == propertyName);
if (exactMatch != null)
return path + exactMatch.DestinationProperty.Name;
foreach (var map in maps)
{
if (map.SourceMember == null)
{
var result = GetDestinationPath(source, map.DestinationProperty.MemberType, propertyName, path + map.DestinationProperty.Name + ".");
if (result != null)
return result;
}
else
{
if (!(map.SourceMember is PropertyInfo))
continue;
var result = GetDestinationPath((map.SourceMember as PropertyInfo).PropertyType, map.DestinationProperty.MemberType, propertyName, path + map.DestinationProperty.Name);
if (result != null)
return result;
}
}
return null;
}