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;
}
I am having 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 a dictionary and object.
So, Please suggest me how can i do this
AutoMapper is quite a flexible solution. You could probably achieve this using a custom mapping profile, e.g.:
With this, I can create a custom instance of a mapper:
Which I could probably do:
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):
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?Much simpler solution. Just map your object from KeyValuePair. Example: