"c# map dictionary to object properties" 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 "c# map dictionary to object properties" answered properly. Developers are finding an appropriate answer about c# map dictionary to object properties related to the C# coding language. By visiting this online portal developers get answers concerning C# codes question like c# map dictionary to object properties. Enter your desired code related query in the search bar and get every piece of information about C# code related question on c# map dictionary to object properties.
c# dictionary values to array
// dict is Dictionary<string, Foo>
Foo[] foos = new Foo[dict.Count];
dict.Values.CopyTo(foos, 0);
// or in C# 3.0:
var foos = dict.Values.ToArray();
Source: stackoverflow.com
mapping dictionary to object c#
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace WebOpsApi.Shared.Helpers
{
public static class MappingExtension
{
public static T ToObject<T>(this IDictionary<string, object> source)
where T : class, new()
{
var someObject = new T();
var someObjectType = someObject.GetType();
foreach (var item in source)
{
var key = char.ToUpper(item.Key[0]) + item.Key.Substring(1);
var targetProperty = someObjectType.GetProperty(key);
if (targetProperty.PropertyType == typeof (string))
{
targetProperty.SetValue(someObject, item.Value);
}
else
{
var parseMethod = targetProperty.PropertyType.GetMethod("TryParse",
BindingFlags.Public | BindingFlags.Static, null,
new[] {typeof (string), targetProperty.PropertyType.MakeByRefType()}, null);
if (parseMethod != null)
{
var parameters = new[] { item.Value, null };
var success = (bool)parseMethod.Invoke(null, parameters);
if (success)
{
targetProperty.SetValue(someObject, parameters[1]);
}
}
}
}
return someObject;
}
public static IDictionary<string, object> AsDictionary(this object source, BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)
{
return source.GetType().GetProperties(bindingAttr).ToDictionary
(
propInfo => propInfo.Name,
propInfo => propInfo.GetValue(source, null)
);
}
}
}
Source: stackoverflow.com
c# map dictionary to object properties
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 c# map dictionary to object properties can get a collection of related answers to their query. Programmers need to enter their query on c# map dictionary to object properties related to C# code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about c# map dictionary to object properties for the programmers working on C# code while coding their module. Coders are also allowed to rectify already present answers of c# map dictionary to object properties while working on the C# language code. Developers can add up suggestions if they deem fit any other answer relating to "c# map dictionary to object properties". Visit this developer's friendly online web community, CodeProZone, and get your queries like c# map dictionary to object properties resolved professionally and stay updated to the latest C# updates.