I have a class like
public User class
{
public string Name{get;set;}
public string Age{get;set;
}
With a dictionary like
Dictionary<string,string> data= new Dictionary<string,string>();
data.Add("Name","Rusi");
data.Add("Age","23");
User user= new User();
Now i want to map User object to this dictionary using Automapper. Automapper maps properties of objects but in my case there is a dictionary and object.
How can this be mapped?
AutoMapper maps between properties of objects and is not supposed to operate in such scenarios. In this case you need Reflection magic. You could cheat by an intermediate serialization:
And if you insist on using AutoMapper you could for example do something along the lines of:
and then:
If you need to handle more complex object hierarchies with sub-objects you must ask yourself the following question: Is
Dictionary<string, string>the correct data structure to use in this case?