如何将对象转换为字典? [英] How do I cast object to dictionary?

查看:72
本文介绍了如何将对象转换为字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将对象转换为字典?



字典键&价值可以是任何类型。



我尝试过:



How do i cast an object to dictionary?

The dictionary key & value can be any type.

What I have tried:

public class Example {

    void CallMethod () {
        SomeMethod (new Dictionary<string, bool> ());
        SomeMethod (new Dictionary<int, long> ());
        SomeMethod<Dictionary<string, bool>> (new Dictionary<string, bool> ());
    }
    
    void SomeMethod (object value) {
        var dic = value as Dictionary<,>;
        // What is type of Dictionary key or value?
    }

    void SomeMethd<T> (T value) {
        // T can be Dictionary, int, bool, List, ...
        if (value is Dictionary) {
            // T is dictionary, but we don't know type of key and value
            // How do i cast it to dictionary?
            var dic = value as Dictionary<,>;
            foreach (var item in dic) {
                
            }
        }
    }

    T SomeMethod<T> () {
        // I want to load the dictionary here
        if (typeof(T) is IDictionary) {
            // Now, what is type of the key and value?
            IDictionary dic = new Dictionary<,> ();
            int count = 1;
            for (int i = 0; i < count; i++) {
                dic.Add (SomeMethod<Type of Key> (), SomeMethod<Type of value> ());
            }
            // Throws an exception
            return dic;
        }
        return default(T);
    }
    
}

推荐答案

除非你知道字典的类型 - 这意味着键和值 - 唯一的方法是将它转换为Dictionary< object,object> - 这是一个非常糟糕的主意,因为它摆脱了强大的打字,使C#如此强大。在这一点上,你依靠运行时强制转换来使用你创建的字典,这是一个糟糕的主意。



您是否考虑过使用泛型,所以你不要你必须施放你的词典,你在调用方法时会推断出类型吗?

Unless you know the type of the dictionary - and that means key and value - the only way to do it is to cast it to a Dictionary<object, object> - which is a very bad idea as it gets rid of the strong typing which makes C# so robust. At this point you are relying on runtime-casts to use the dictionary you create, and that's a poor idea.

Have you instead considered using Generics, so you don't have to cast your Dictionary, the types are inferred when you call the method?
void SomeMethod<K, V>(Dictionary<K, V> dict)
    {
    ///...
    }

它会使你的代码更健壮,更容易阅读。

It'll make your code both more robust, and a lot easier to read.


根据您在 SomeMethod()方法中对类型信息的重要程度,您可以执行以下操作:

Depending on how much the type information is important to you within the SomeMethod() method you can do:
// minimal type info needed
void SomeMethod (System.Collections.IDictionary dictionary)
{
   foreach(var k in dictionary.Keys)
   {
      // k and dictionary[k] as value
   }
}



或者如果需要类型:


Or if the types are needed:

// overloaded for each case
void SomeMethod (System.Collections.Generic.Dictionary<int,long> dictionary)
{
   foreach(var kv in dictionary)
   {
      //access to kv.Key and kv.Value as int and long
   }
}

void SomeMethod (System.Collections.Generic.Dictionary<string,bool> dictionary)
{
   foreach(var kv in dictionary)
   {
      //access to kv.Key and kv.Value as string and bool
   }
}


Hope ,我正确地得到了问题。对象到Dictonary。它运行没有错误。



Hope, i got the question correctly. Object to Dictonary. It runs without error.

var dic = value as Dictionary<object,object>;


这篇关于如何将对象转换为字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆