在使用PropertyInfo的GetValue获取值的时候
发现GetValue返回的值是一个List<T>类型。
此时我没办法读取出这个List<T>的内容。
例子代码如下class Program
    {
        static void Main(string[] args)
        {
            List<ClassB> family = new List<ClassB>();
            family.Add(new ClassB("张一", "M"));
            family.Add(new ClassB("张二", "M"));            List<ClassA> Data = new List<ClassA>();
            Data.Add(new ClassA("张三", family));
            ReadClass(Data);
            
        }        public static void ReadClass<T>(List<T> Data)
        {
                        foreach (object obj in Data)
            {
                PropertyInfo[] oProps = obj.GetType().GetProperties();
                foreach (PropertyInfo p in oProps)
                {
                    //这里输出ClassA的Name是没问题的。
                    //但是family在ClassA中是一个List此处程序会报错,所以用一个IF跳过。
                    //请高手指教一下怎么在这各种情况下获取family这个List的值
                    if (p.PropertyType.Name != System.Type.GetType("System.Collections.Generic.List`1").Name)
                    {
                        string sTmp = p.Name + ":" + p.GetValue(obj, null);
                        Console.WriteLine(sTmp);
                    }
                    else
                    {
                        //List<T>的处理方式
                    }                }            }
        }
    
    }
    class ClassA
    {       
        public ClassA(string Name,List<ClassB> family)
        {
            this.Name = Name;
            this.family = family;
        }
        public string Name { set; get; }
        public List<ClassB> family { set; get; }
    }    class ClassB
    {
        public ClassB(string Name, string Gerder)
        {
            this.Name = Name;
            this.Gender = Gender;
        }        public string Name {set;get;}
        public string Gender {set;get;}
    }

解决方案 »

  1.   

    做个类型转换就可以了呀
    List<ClassB> listB = (p.GetValue(obj, null) as List<ClassB>);
    foreach (var b in listB)
    {
        Console.WriteLine("Name:" + b.Name + "  Gender:" + b.Gender);
    }
      

  2.   

                        if (p.PropertyType.IsGenericType)
                        {
                            ReadClass((List<ClassB>)p.GetValue(obj, null));
                        }
                        else
                        {
                            string sTmp = p.Name + ":" + p.GetValue(obj, null);
                            Console.WriteLine(sTmp);
                        }
      

  3.   

    1:PropertyInfo.GetValue()返回的并不是List,而是类本身的属性或者字段信息
    2:对于楼主的需求,给出一个稍微简单的例子作为参考/// <summary>
            /// 返回相信信息
            /// </summary>
            /// <returns></returns>
            public static string GetPropertyInfo()
            {
                Student stu1 = new Student { ID = 1001, Code = "S001", Name = "谌豹" };
                Student stu2 = new Student { ID = 1002, Code = "S002", Name = "郑巧玲" };
                List<string> results = new List<string>();
                string result=string.Empty;            System.Reflection.PropertyInfo[] pros = typeof(Student).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
                foreach (System.Reflection.PropertyInfo pro in pros)
                {
                    string value = pro.GetValue(stu1, null).ToString();
                    results.Add(value);                if (pro.Name == "ID")
                        result += "ID:" + value + "  ";
                    else if (pro.Name == "Code")
                        result += "Code:" + value + "  ";
                    else if (pro.Name == "Name")
                        result += "Name:" + value;
                }            return result;
            }