Rus*_*ova 17 c# asp.net-mvc dictionary automapper asp.net-mvc-3
以下代码仅适用于此问题
我正在上课
public User class
{
public string Name{get;set;}
public string Age{get;set;
}
Run Code Online (Sandbox Code Playgroud)
我有一本字典
Dictionary<string,string> data= new Dictionary<string,string>();
data.Add("Name","Rusi");
data.Add("Age","23");
User user= new User();
Run Code Online (Sandbox Code Playgroud)
现在我想 使用User
对象映射 到此.Automapper映射对象的属性,但在我的例子中有一个字典和对象.dictionary
Automapper
所以,请建议我怎么做
Dar*_*rov 24
AutoMapper在对象的属性之间进行映射,并且不应该在这种情况下运行.在这种情况下,你需要反射魔法.您可以通过中间序列化作弊:
var data = new Dictionary<string, string>();
data.Add("Name", "Rusi");
data.Add("Age", "23");
var serializer = new JavaScriptSerializer();
var user = serializer.Deserialize<User>(serializer.Serialize(data));
Run Code Online (Sandbox Code Playgroud)
如果你坚持使用AutoMapper,你可以做一些事情:
Mapper
.CreateMap<Dictionary<string, string>, User>()
.ConvertUsing(x =>
{
var serializer = new JavaScriptSerializer();
return serializer.Deserialize<User>(serializer.Serialize(x));
});
Run Code Online (Sandbox Code Playgroud)
然后:
var data = new Dictionary<string, string>();
data.Add("Name", "Rusi");
data.Add("Age", "23");
var user = Mapper.Map<Dictionary<string, string>, User>(data);
Run Code Online (Sandbox Code Playgroud)
如果需要使用子对象处理更复杂的对象层次结构,则必须问自己以下问题:Dictionary<string, string>
在这种情况下使用正确的数据结构吗?
这个线程有点旧,但现在有如何在没有任何配置的情况下在 automapper 上做到这一点,如官方文档所述:
AutoMapper 可以在没有任何显式配置的情况下映射到/从动态对象 (...) 同样,您可以直接从 Dictionary 映射到对象,AutoMapper 会将键与属性名称对齐。
更新:
以下代码显示了一个工作示例(带有单元测试)。
void Test()
{
var mapper = new MapperConfiguration(cfg => { }).CreateMapper();
var dictionary = new Dictionary<string, object>()
{
{ "Id", 1 },
{ "Description", "test" }
};
var product = mapper.Map<Product>(dictionary);
Assert.IsNotNull(product);
Assert.AreEqual(product.Id, 1);
Assert.AreEqual(product.Description, "test");
}
class Product
{
public int Id { get; set; }
public string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
小智 8
因为我刚刚偶然发现了这个问题,所以我想在当前版本的 AutoMapper 中添加这个答案(即使原始问题已经很老了):
public class MyConfig
{
public string Foo { get; set; }
public int Bar { get; set; }
}
var config = new MapperConfiguration(cfg => {});
var mapper = config.CreateMapper();
var source = new Dictionary<string, object>
{
["Foo"] = "Hello",
["Bar"] = 123
};
var obj = mapper.Map<MyConfig>(source);
obj.Foo == "Hello"; // true
Run Code Online (Sandbox Code Playgroud)
AutoMapper是一个非常灵活的解决方案.您可以使用自定义映射配置文件实现此目的,例如:
public class UserMappingProfile : Profile
{
// Props
public override string ProfileName { get { return "UserMappingProfile"; } }
// Methods
public override void Configure()
{
CreateMap<User, Dictionary<string, string>>().ConvertUsing<DictionaryTypeConverter>();
base.Configure();
}
// Types
internal class DictionaryTypeConverter : ITypeConverter<User, Dictionary<string, string>>
{
public User Convert(ResolutionContext context)
{
var dict = context.SourceValue as Dictionary<string, string>;
if (dict == null)
return null;
return new User() { Name = dict["Name"], Age = dict["Age"] };
}
}
}
Run Code Online (Sandbox Code Playgroud)
有了这个,我可以创建一个mapper的自定义实例:
var config = new Configuration(new TypeMapFactory(), MapperRegistry.AllMappers());
config.AddProfile<UserMappingProfile>();
config.AssertConfigurationIsValid();
var mapper = new MappingEngine(config);
Run Code Online (Sandbox Code Playgroud)
我可能做的事情:
var dict = new Dictionary<string, string> { { "Name", "Matt" }, { "Age", "27" } };
var user = mapper.Map<User, Dictionary<string, string>>(dict);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
31746 次 |
最近记录: |