我有一個項目,我試圖將一個字典映射到ViewModel.NamedProperty。我正在嘗試使用AutoMapper自定義解析器來執行基於約定的映射。我的約定是,如果源字典鍵的命名屬性存在,則映射字典值的屬性。這裏是我的示例類:基於Automapper約定的映射集合
class User
{
string Name {get;set;}
Dictionary<string, AccountProp> CustomProperties {get;set;}
}
class AccountProp
{
string PropertyValue {get;set;}
//Some other properties
}
class UserViewModel
{
string Name {get;set;}
DateTime LastLogin {get;set;}
string City {get;set}
}
var user = new User()
{
Name = "Bob"
};
user.CustomProperties.Add("LastLogin", new AccountProp(){PropertyValue = DateTime.Now};
user.CustomProperties.Add("City", new AccountProp(){PropertyValue = "SomeWhere"};
我想通過約定來映射用戶CustomProperties來字典扁平UserViewModel所有屬性的,我不想單獨指定每個屬性的映射。
什麼是最好的方式去做這件事?我在想自定義值解析器,但似乎我必須指定每個成員我想單獨映射。如果我想這樣做,我只需手動執行沒有AutoMapper的映射。
謝謝,但我不想像我原來的文章中指定的那樣映射單個屬性。我知道我可以使用ForMember和MapFrom以這種方式工作。我希望它按照慣例使用通用的TypeConverter或其他Automapper慣例進行映射,因爲不止有一些屬性。 – Jay