Can I convert Class into Dictionary<string, string>?
In Dictionary I want my class properties as keys and value of particular a property as the value.
Suppose my class is
public class Location
{
public string city { get; set; }
public string state { get; set; }
public string country { get; set;
}
Now suppose my data is
city = Delhi
state = Delhi
country = India
Now you can understand my point easily!
I want to make a Dictionary! That dictionary should be like:
Dictionary<string,string> dix = new Dictionary<string,string> ();
dix.add("property_name", "property_value");
I can get the value! But how can I get property names (not value)?
What should I code to create it dynamic? That should work for every class which I want.
You can understand this question as:
How can I get a list of properties from particular class?
Now again I am explaining one of my eagernesses about Dictionary! This question popuped in my mind from answer of my previous question!!
I found it easy to json serialize the object and deserialize as a dictionary. var json = JsonConvert. SerializeObject(obj); var dictionary = JsonConvert. DeserializeObject<Dictionary<string, string>>(json);
Convert List to Dictionary Using the Non-Linq Method in C# We can also convert a list to a dictionary in a non-LINQ way using a loop. It is advised to use the non-LINQ method because it has improved performance, but we can use either method according to our preferences.
One can only put one type of object into a dictionary. If one wants to put a variety of types of data into the same dictionary, e.g. for configuration information or other common data stores, the superclass of all possible held data types must be used to define the dictionary.
This is the recipe: 1 reflection, 1 LINQ-to-Objects!
someObject.GetType()
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
.ToDictionary(prop => prop.Name, prop => (string)prop.GetValue(someObject, null))
Since I published this answer I've checked that many people found it useful. I invite everyone looking for this simple solution to check another Q&A where I generalized it into an extension method: Mapping object to dictionary and vice versa
Here a example with reflection without LINQ:
Location local = new Location();
local.city = "Lisbon";
local.country = "Portugal";
local.state = "None";
PropertyInfo[] infos = local.GetType().GetProperties();
Dictionary<string,string> dix = new Dictionary<string,string> ();
foreach (PropertyInfo info in infos)
{
dix.Add(info.Name, info.GetValue(local, null).ToString());
}
foreach (string key in dix.Keys)
{
Console.WriteLine("nameProperty: {0}; value: {1}", key, dix[key]);
}
Console.Read();
I would like to add an alternative to reflection, using JToken. You will need to check the benchmark difference between the two to see which has better performance.
var location = new Location() { City = "London" };
var locationToken = JToken.FromObject(location);
var locationObject = locationObject.Value<JObject>();
var locationPropertyList = locationObject.Properties()
.Select(x => new KeyValuePair<string, string>(x.Name, x.Value.ToString()));
Note this method is best for a flat class structure.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With