using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;namespace PropertyTest
{    class A
    {
        public string p1 { get; set; }
    }    class B
    {
        public List<A> p2 { get; set; }
    }
    class Program
    {
        static void Main(string[] args)
        {
            B b = new B();
            A a = new A();
            a.p1 = "test";
            b.p2.Add(a);
            getproperty(b);
        }        static void getproperty<T>(T t)
        {
            Type type = t.GetType();
            foreach (PropertyInfo info in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                object value = info.GetValue(t, null);
                if (info.PropertyType.IsClass && info.PropertyType.ToString() != "System.String")
                {
                    if (info.PropertyType.IsGenericType)
                    {
                        //这时候这个T就已经实例成 类B 导致 强制转换失败 求教一下 该怎么处理
                        foreach (T te in (List<T>)value)
                        {
                            getproperty(te);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("{0}={1}", info.Name, value.ToString());
                }
            }
        }
    }
}问题出现在红色注释 系统报错:无法将类型为“System.Collections.Generic.List`1[PropertyTest.A]”的对象强制转换为类型“System.Collections.Generic.List`1[PropertyTest.B]”。在线等。。

解决方案 »

  1.   

    估计你没看明白, object value = info.GetValue(t, null);
    这个value 实际上就是刚才b.p2 也就是List<A>
      

  2.   

    我并不是要转换A 和 B 
    我是要读出所有B的属性。B的P2属性是A的一个泛型集合
    我需要遍历出B 及 A 的属性,所以我用了递归 遍历所有属性而已。。
      

  3.   

    可能我的思路有问题,有没有办法能将B A 两个类的属性全部输出出来。
    由于可能会有其他类所以方法里并具体new B 或者 new A
    如果分少可以额外加分的。。
      

  4.   

     static void Main(string[] args)
            {
                B b = new B(){p2=new List<A>()};
                A a = new A();
                a.p1 = "test";
                b.p2.Add(a);
                getproperty(b);
            }        static void getproperty<T>(T t)
            {
                Type type = t.GetType();
                foreach (PropertyInfo info in type.GetProperties(BindingFlags.Instance | BindingFlags.Public))
                {
                    object value = info.GetValue(t, null);
                    if (info.PropertyType.IsClass && info.PropertyType.ToString() != "System.String")
                    {
                        if (info.PropertyType.IsGenericType)
                        {
                            //这时候这个T就已经实例成 类B 导致 强制转换失败 求教一下 该怎么处理
                            foreach (var te in (List<A>)value)
                            {
                                getproperty(te);
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("{0}={1}", info.Name, value.ToString());
                    }
                }
            }
      

  5.   

    foreach (var te in (IEnumerable)value)