How to convert object to Dictionary in C#?

前端 未结 15 1691
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 17:58

How do I convert a dynamic object to a Dictionary in C# What can I do?

public static void MyMethod(object obj)
{
    if (typ         


        
相关标签:
15条回答
  • 2020-12-13 18:02

    Simple way:

    public IDictionary<T, V> toDictionary<T, V>(Object objAttached)
    {
        var dicCurrent = new Dictionary<T, V>();
        foreach (DictionaryEntry dicData in (objAttached as IDictionary))
        {
            dicCurrent.Add((T)dicData.Key, (V)dicData.Value);
        }
        return dicCurrent;
    }
    
    0 讨论(0)
  • 2020-12-13 18:03
       public static void MyMethod(object obj){
       Dictionary<string, string> dicEditdata = data as Dictionary<string, string>;
       string abc=dicEditdata["id"].ToString();} 
    

    suppose--- if you place the cursor over the object(obj) while debugging and if you get an object with the value {['id':'ID1003']} then you can use the value as

    string abc=dicEditdata["id"].ToString(); 
    
    0 讨论(0)
  • 2020-12-13 18:03

    If you mind to use LINQ Expression;

    public static Dictionary<string, object> ConvertFromObjectToDictionary(object arg)
    {
         return arg.GetType().GetProperties().ToDictionary(property => property.Name, property => property.GetValue(arg));
    }
    
    0 讨论(0)
  • 2020-12-13 18:07

    You can use this:

    Dictionary<object,object> mydic = ((IEnumerable)obj).Cast<object>().ToList().ToDictionary(px => px.GetType().GetProperty("Key").GetValue(px), pv => pv.GetType().GetProperty("Value").GetValue(pv));
    
    0 讨论(0)
  • 2020-12-13 18:08

    Another option is to use NewtonSoft.JSON.

    var dictionary = JObject.FromObject(anObject).ToObject<Dictionary<string, object>>();
    
    0 讨论(0)
  • 2020-12-13 18:10
        public static KeyValuePair<object, object > Cast<K, V>(this KeyValuePair<K, V> kvp)
        {
            return new KeyValuePair<object, object>(kvp.Key, kvp.Value);
        }
    
        public static KeyValuePair<T, V> CastFrom<T, V>(Object obj)
        {
            return (KeyValuePair<T, V>) obj;
        }
    
        public static KeyValuePair<object , object > CastFrom(Object obj)
        {
            var type = obj.GetType();
            if (type.IsGenericType)
            {
                if (type == typeof (KeyValuePair<,>))
                {
                    var key = type.GetProperty("Key");
                    var value = type.GetProperty("Value");
                    var keyObj = key.GetValue(obj, null);
                    var valueObj = value.GetValue(obj, null);
                    return new KeyValuePair<object, object>(keyObj, valueObj);
                }
            }
            throw new ArgumentException(" ### -> public static KeyValuePair<object , object > CastFrom(Object obj) : Error : obj argument must be KeyValuePair<,>");
        }
    

    From the OP:

    Instead of converting my whole Dictionary, i decided to keep my obj dynamic the whole time. When i access the keys and values of my Dictionary with a foreach later, i use foreach(dynamic key in obj.Keys) and convert the keys and values to strings simply.

    0 讨论(0)
提交回复
热议问题