Dictionary map to an object using Automapper

前端 未结 6 552
执笔经年
执笔经年 2020-11-29 07:50

Below code is just for this question

I am having a class like

public User class
{
 public string Name{get;set;}
 public string Age{get;set;
}
         


        
6条回答
  •  隐瞒了意图╮
    2020-11-29 08:09

    As I've just stumbled upon this question I'd like to add this answer possible with the current version of AutoMapper (even if the original question is already pretty old):

    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
    {
        ["Foo"] = "Hello",
        ["Bar"] = 123
    };
    var obj = mapper.Map(source);
    obj.Foo == "Hello"; // true
    

提交回复
热议问题