"convert dictionary to object c#" Code Answer's
You're definitely familiar with the best coding language C# that developers use to develop their projects and they get all their queries like "convert dictionary to object c#" answered properly. Developers are finding an appropriate answer about convert dictionary to object c# related to the C# coding language. By visiting this online portal developers get answers concerning C# codes question like convert dictionary to object c#. Enter your desired code related query in the search bar and get every piece of information about C# code related question on convert dictionary to object c#.
convert dictionary to object c#
class ObjectToMapTo
{
public int ID;
public string Name;
public bool IsAdmin;
public override string ToString()
{
return $"(ID={ID} Name={Name} IsAdmin={IsAdmin})";
}
}
static object MapDictToObj(Dictionary<string, object> dict, Type destObject)
{
object returnobj = Activator.CreateInstance(destObject);
foreach (string key in dict.Keys)
{
object value = dict[key];
FieldInfo field = destObject.GetField(key);
if (field != null)
{
field.SetValue(returnobj, value);
}
}
return returnobj;
}
static void Main(string[] args)
{
Dictionary<string, object> dict = new Dictionary<string, object>();
dict["ID"] = 1000;
dict["Name"] = "This is a name";
dict["IsAdmin"] = true;
ObjectToMapTo obj = (ObjectToMapTo)MapDictToObj(dict, typeof(ObjectToMapTo));
Console.WriteLine(obj);
Console.ReadKey();
//Returns: (ID=1000 Name=This is a name IsAdmin=True)
}
All those coders who are working on the C# based application and are stuck on convert dictionary to object c# can get a collection of related answers to their query. Programmers need to enter their query on convert dictionary to object c# related to C# code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about convert dictionary to object c# for the programmers working on C# code while coding their module. Coders are also allowed to rectify already present answers of convert dictionary to object c# while working on the C# language code. Developers can add up suggestions if they deem fit any other answer relating to "convert dictionary to object c#". Visit this developer's friendly online web community, CodeProZone, and get your queries like convert dictionary to object c# resolved professionally and stay updated to the latest C# updates.