99

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!!

6
  • Can you try to reword the question please? I have no real idea what you are trying to achieve. Commented Feb 9, 2012 at 12:00
  • Insort ! I want all variables' name of particular class ! Is this possible ?
    – Chintan
    Commented Feb 9, 2012 at 12:02
  • @NoOne I've answered you, but you need to learn something more about OOP. You're talking about properties. And class-level "variables" are called fields. It's important to call everything by its name as understanding you'd easier than now! ;) Commented Feb 9, 2012 at 12:06
  • @MatíasFidemraizer Sorry ! My mistake I should use "property" I have edited !
    – Chintan
    Commented Feb 9, 2012 at 12:11
  • @Chintan I dont know where you plan on using a dictionary of a class, note, that in contrary to your class where "Country" can be a property of type Country and city is of type string, in a dictionary you have to specify the most basic type you are going to store (most likely object) which will make your life harder further along the road.
    – Tomer W
    Commented Jan 16, 2017 at 13:17

9 Answers 9

254

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

7
  • what if I see "intrinsic" properties like "GenericEqualityComparer", rather than properties I explicitly declared? I've tried BindingFlags.DeclaredOnly.
    – drzaus
    Commented Jul 17, 2012 at 15:43
  • UPDATE nevermind - I found that the method using this reflection (itself in an object extension) ended up calling itself with the converted result, so my extension was getting called on an object AND THEN on the resulting dictionary.
    – drzaus
    Commented Jul 17, 2012 at 15:54
  • I can't compile this in Unity3D, am I missing an assembly reference?
    – C0D3
    Commented Dec 10, 2013 at 19:10
  • 1
    @TimWilliams did you miss the cast to string? Commented Nov 24, 2020 at 19:35
  • 2
    Although not all properties will cast to string, so perhaps it's better to do this: prop => prop.GetValue(o, null)?.ToString() ?? ""
    – John M
    Commented Sep 23, 2021 at 11:54
29

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();
2
  • This saved me at least a days worth of work. Thanks so much I would vote you up twice if I could!
    – Kibitz503
    Commented Nov 29, 2012 at 21:54
  • info.GetValue() may return null if the underlying property is null, so ToString() can throw an exception, might be worth to check the return value.
    – Jay
    Commented May 5, 2022 at 4:40
11
public static Dictionary<string, object> ToDictionary(object model)
    {
        var serializedModel = JsonModelSerializer.Serialize(model);
        return JsonModelSerializer.Deserialize<Dictionary<string, object>>(serializedModel);
    }

I have used the above code. As simplified as possible and it works without reflection and the model could be nested and still work. (Change your code to not use Newtonsoft if you are using Json.net)

2
  • I'd imagine this method to be far less efficient (and I'm certain JsonModelSerializer is using reflection internally... So it doesn't really work without reflection.)
    – R J
    Commented Apr 5, 2021 at 8:34
  • @RJ, yes, but I guess NewtonSoft (and others) uses reflection in a very optimized way, with enough testing and benchmarking, and they have more chances to optimize than a single programmer implementing reflction directly. I already use similar method to clone classes and doesn't harm the general performance.
    – zameb
    Commented Sep 4, 2023 at 14:37
2

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.

2
  • var locationObject = locationObject.Value<JObject>();? Commented Feb 28, 2018 at 4:01
  • Here's a more compact version: var props = JToken.FromObject(query) .Value<JObject>() .Properties() .ToDictionary(k => k.Name, v => $"{v.Value}");
    – Makotosan
    Commented Mar 21, 2019 at 13:34
2

Give this a try.

This uses reflection to retrieve the object type, then all of the properties of the supplied object (PropertyInfo prop in obj.GetType().GetProperties(), see https://learn.microsoft.com/en-us/dotnet/api/system.type.getproperties?view=netcore-3.1). It then adds the property name as a key in the dictionary and, if a value exists, it adds the value, otherwise it adds null.

    public static Dictionary<string, object> ObjectToDictionary(object obj)
    {
        Dictionary<string, object> ret = new Dictionary<string, object>();

        foreach (PropertyInfo prop in obj.GetType().GetProperties())
        {
            string propName = prop.Name;
            var val = obj.GetType().GetProperty(propName).GetValue(obj, null);
            if (val != null)
            {
                ret.Add(propName, val);
            }
            else
            {
                ret.Add(propName, null);
            }
        }

        return ret;
    }
0
0
protected string getExamTimeBlock(object dataItem)
{
    var dt = ((System.Collections.Specialized.StringDictionary)(dataItem));

    if (SPContext.Current.Web.CurrencyLocaleID == 1033) return dt["en"];
    else return dt["sv"];
}
1
  • An explanation would be in order. Commented Oct 6, 2020 at 12:34
0

Just a gift for someone only need a simple and flat Dictionary<String,String> not requiring hierarchy or deserialize back to an object like me 🌳

    private static readonly IDictionary<string, string> SPECIAL_FILTER_DICT = new Dictionary<string, string>
    {
        { nameof(YourEntityClass.ComplexAndCostProperty), "Some display text instead"},
        { nameof(YourEntityClass.Base64Image), ""},
        //...
    };


    public static IDictionary<string, string> AsDictionary(this object source, BindingFlags bindingAttr = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance)
    {
        if (source == null)
            return new Dictionary<string, string> {
                {"",""}
            };

        return source.GetType().GetProperties(bindingAttr).ToDictionary
        (
            propInfo => propInfo.Name,
            propInfo => propInfo.GetValue(source, null).GetSafeStringValue(propInfo.Name)
        );
    }


    public static String GetSafeStringValue(this object obj, String fieldName)
    {
        if (obj == null)
            return "";

        if (obj is DateTime)
            return GetStringValue((DateTime)obj);

        // More specical convert...

        if (SPECIAL_FILTER_DICT.ContainsKey(fieldName))
            return SPECIAL_FILTER_DICT[fieldName];

        // Override ToString() method if needs
        return obj.ToString();
    }


    private static String GetStringValue(DateTime dateTime)
    {
        return dateTime.ToString("YOUR DATETIME FORMAT");
    }
0

I hope this extension can be useful to someone.

public static class Ext {
    public static Dictionary<string, object> ToDict<T>(this T target)
        => target is null
            ? new Dictionary<string, object>()
            : typeof(T).GetProperties(BindingFlags.Instance | BindingFlags.Public)
                        .ToDictionary(
                            x => x.Name,
                            x => x.GetValue(target)
                        );
}
2
  • 1
    It works but I'd argue if this is an extension method abstracting the logic the syntax of this is over compressed and obscured by the language "sugar". In a scenario like this I'd leave the logic in it's more verbose format as it makes it much easier to read.
    – Oliver
    Commented Aug 12, 2020 at 16:20
  • An explanation would be in order. Commented Oct 6, 2020 at 12:34
0
public static Dictionary<string, string> ObjectToDictionary(object obj)
{
    return Newtonsoft.Json.Linq.JObject.FromObject(obj).ToObject<Dictionary<string, string>>();
}
1
  • Thank you for your interest in contributing to the Stack Overflow community. This question already has quite a few answers—including one that has been extensively validated by the community. Are you certain your approach hasn’t been given previously? If so, it would be useful to explain how your approach is different, under what circumstances your approach might be preferred, and/or why you think the previous answers aren’t sufficient. Can you kindly edit your answer to offer an explanation? Commented Aug 14, 2023 at 0:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.