none
我想用反射循环调用 Dictionary<TKey, TValue> 的值, 但是这个 Dictionary<TKey, TValue> 的T 是不定的. RRS feed

  • 問題

  • 下面是我的测试代码:

    Code Snippet

        public class TestDictionaryClass
        {
            public Dictionary<int, System.Drawing.Size> Dic = new Dictionary<int, System.Drawing.Size>();
            public Dictionary<string, System.Drawing.Size> DicStr = new Dictionary<string, System.Drawing.Size>();

            public static void TestDictionary()
            {
                TestDictionaryClass one = new TestDictionaryClass();
                one.Dic.Add(99, new Size(1, 0));
                one.Dic.Add(80, new Size(12, 8));

                ForeachDictionaryValue(one,"Dic");

                //----------------------------------------------
                //ForeachDictionaryValue 怎么样才能够处理不用 T 的 Dictionary<TKey, TValue> ?

                TestDictionaryClass two = new TestDictionaryClass();
                two.DicStr.Add("A", new Size(1, 0));

                //怎样改 ForeachDictionaryValue 的逻辑才能够处理下面的代码.
                //ForeachDictionaryValue(two, "DicStr");
               
            }

            public static void ForeachDictionaryValue(object obj,string fieldName)
            {
                FieldInfo field = obj.GetType().GetField(fieldName);

                //Get the IEnumerable interface from the field.
                IEnumerable IEnum = (IEnumerable)field.GetValue(obj);

                foreach (KeyValuePair<int, System.Drawing.Size> de in IEnum)
                {
                    Console.WriteLine(" Key = {0}, Value = {1}", de.Key, de.Value);
                }

            }
        }

     

     

    我想用反射循环调用 Dictionary<TKey, TValue> 的值, 但是这个 Dictionary<TKey, TValue> 的T 是不定的.

    主要问题在于,遍历Dictionary<TKey, TValue>是返回一个 struct KeyValuePair<TKey, TValue>.

     

    我的问题:

    1. 我先知道怎么样可以知道一个Field 是否是  Dictionary<TKey, TValue>
    2. 如何用公共的代码,去遍历 Tkey 和 TValue 都不固定的  Dictionary<TKey, TValue>
    2008年12月26日 上午 02:43

所有回覆

  • 如下是粗劣的解決手法, 請各位高手勿見笑...

     

    問題一: 請參閱藍色部份

    問題二: 請參閱紅色部份(她不知道,  您就先給她個明示呗...)

     

    Code Snippet

        public class TestDictionaryClass
        {
            public Dictionary<int, System.Drawing.Size> Dic = new Dictionary<int, System.Drawing.Size>();
            public Dictionary<string, System.Drawing.Size> DicStr = new Dictionary<string, System.Drawing.Size>();

            public static void TestDictionary()
            {
                TestDictionaryClass one = new TestDictionaryClass();
                one.Dic.Add(99, new Size(1, 0));
                one.Dic.Add(80, new Size(12, 8));

                ForeachDictionaryValue<int, System.Drawing.Size>(one, "Dic");

                //----------------------------------------------
                //ForeachDictionaryValue 怎么样才能够处理不用 T 的 Dictionary<TKey, TValue> ?
                TestDictionaryClass two = new TestDictionaryClass();
                two.DicStr.Add("A", new Size(1, 0));

                //怎样改 ForeachDictionaryValue 的逻辑才能够处理下面的代码.
                ForeachDictionaryValue<string, System.Drawing.Size>(two, "DicStr");           


                //回應你的問題1 所另外另外弄的範例
                FieldInfo[] sField = GetFields<Dictionary<string, System.Drawing.Size>>(two);

            }

            public static void ForeachDictionaryValue<T,K>(object obj, string fieldName)
            {
                FieldInfo field = obj.GetType().GetField(fieldName);


                //Get the IEnumerable interface from the field.
                IEnumerable IEnum = (IEnumerable)field.GetValue(obj);

                foreach (KeyValuePair<T, K> de in IEnum)
                {
                    Console.WriteLine(" Key = {0}, Value = {1}", de.Key, de.Value);
                }
            }

            public static FieldInfo[] GetFields<T>(object obj)
            {
                List<FieldInfo> fieldList = new List<FieldInfo>();

                FieldInfo[] fields = obj.GetType().GetFields();

                foreach (FieldInfo field in fields)
                {
                    if (IsDictionaryType<T>(field.FieldType))
                    {
                        fieldList.Add(field);
                    }
                }

                return fieldList.ToArray();
            }

            public static bool IsDictionaryType<T>(Type type)
            {
                bool retBool = false;
                Type t = typeof(T);

                if (type == t)
                    retBool = true;

                return retBool;
            }

        }

     

     

    2009年1月2日 上午 06:38