locked
How to convert object into Dictionary RRS feed

  • Question

  • User321489315 posted

    Hello All, 

    Please help me to convert below "Order" object into dictionary.  

    public class Order
        {
            #region Properties      
            public byte? IsMarried
            {
                get;
                set;
            }        
            public byte? IsEmployed
            {
                get;
                set;
            }        
            public byte? IsStudent
            {
                get;
                set;
            }      
            public string Name
            {
                get;
                set;
            }
    }
    
    ----------------------
    public class OrderManager : Order
    {
     private Dictionary<string, object> preferences;
    }

    How can i convert order object as dictionary(Key/Value pair) in OrderManager class.

    Thanks in Advance,

    Chandu

    Tuesday, March 13, 2018 5:21 AM

All replies

  • User516094431 posted

    As per you query, I recommend you already discussed thread on same topic.

    how to convert an object of Item to an dictionary not idictionary in c#

    Other Refer Link:

    https://gist.github.com/jarrettmeyer/798667/a87f9bcac2ec68541511f17da3c244c0e05bdc49

    Tuesday, March 13, 2018 6:31 AM
  • User-37275327 posted

    Have a look below,

    var preferences = new Dictionary<string, Order>       
    {       
      {"1", new Order() {IsMarried = "Yes", IsEmployed = "Yes", IsStudent = "Yes", Name="Yes"}},       
      {"2", new Order() {IsMarried = "Yes", IsEmployed = "Yes", IsStudent = "Yes", Name="Yes"}}       
        
    };

    Tuesday, March 13, 2018 10:02 AM
  • User-707554951 posted

    Hi chandu_king

    Related code as below:

    public static class ObjectToDictionaryHelper
    {
      public static IDictionary<string, object> ToDictionary(this object source)
      {
        return source.ToDictionary<object>();
      }
    
      public static IDictionary<string, T> ToDictionary<T>(this object source)
      {
        if (source == null) ThrowExceptionWhenSourceArgumentIsNull();
    
        var dictionary = new Dictionary<string, T>();
        foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(source))
        {
          object value = property.GetValue(source);
          if (IsOfType<T>(value))
          {
            dictionary.Add(property.Name, (T)value);
          }
        }
        return dictionary;
      }
    
      private static bool IsOfType<T>(object value)
      {
        return value is T;
      }
    
      private static void ThrowExceptionWhenSourceArgumentIsNull()
      {
        throw new NullReferenceException("Unable to convert anonymous object to a dictionary. The source anonymous object is null.");
      }
    }

    Related links:

    https://stackoverflow.com/a/13602785/9143922

    https://gist.github.com/jarrettmeyer/798667/a87f9bcac2ec68541511f17da3c244c0e05bdc49

    Best regards

    Cathy

    Wednesday, March 14, 2018 2:00 AM