研究半天未果。List<xxxxx> abc = new List<xxxxx>;
OutPut(abc);---------------------void OutPut(object o)
{
object o;
System.Type t = o.GetType();
string tmp = t.ToString();
if (tmp.IndexOf("System.Collections.Generic.List") != -1)
{
   string t2Str = tmp.Substring(tmp.IndexOf("[") + 1, tmp.IndexOf("]") - tmp.IndexOf("[") - 1);   System.Type t2 = Type.GetType(t2Str);
   List<object> os = (List<object>)o;   foreach (object o2 in (System.Collections.ArrayList)o)
   {
     ......
   }
}
}
本来打算这么做,不过List<xxxxx>不能转换成List<object>,So ?

解决方案 »

  1.   

    晕 那还不如直接用arraylist
      

  2.   

    List<object> os = (List<object>)o;foreach (object o2 in (System.Collections.ArrayList)o)
    {
    ......
    }
    ------------------》这样就可以吧
    //List<object> os = (List<object>)o;foreach (object o2 in o)
    {
    ......
    }
      

  3.   

    foreach (object o2 in (System.Collections.ArrayList)o)
       {
         ......
       }搞错了,应该是 
     foreach (object o2 in os)
       {
         ......
       }
    不过没什么影响,还是错的。
      

  4.   

    函数改成这样就可以了
    void OutPut(IList o)
      

  5.   

    void OutPut(IList o)
            {
                System.Type t = o.GetType();
                string tmp = t.ToString();
                if (tmp.IndexOf("System.Collections.Generic.List") != -1)
                {
                    string t2Str = tmp.Substring(tmp.IndexOf("[") + 1, tmp.IndexOf("]") - tmp.IndexOf("[") - 1);                System.Type t2 = Type.GetType(t2Str);                foreach (object o2 in o)
                    {
    ...
                    }
                }
            }
      

  6.   

    public static void OutPutObject(object o, int indent)
            {
                if (o != null)
                {
                    Type t = o.GetType();
                    Type[] gt = t.GetGenericArguments();                if (gt.Length == 0)
                    {
                        System.Console.WriteLine("value[" + t.Name + "] = " + o.ToString());
                        System.Console.WriteLine("--------------------------------------------");                    foreach (PropertyInfo pi in t.GetProperties())
                        {
                            object o2 = pi.GetValue(o, null);
                            if (o2 != null)
                            {
                                System.Console.WriteLine(GetIndent(indent) + pi.Name + "[" + pi.PropertyType.Name + "] = " + o2.ToString());
                            }
                        }
                    }
                    else
                    {
                        System.Type t2 = gt[0];                   
                        List<object> os = (List<object>)o;                    foreach (object o2 in os)
                        {
                            OutPutObject(o2, indent + 1);
                        }
                    }
                }
                else
                {
                    System.Console.WriteLine("null");
                }
            }
      

  7.   

    public static void OutPutObject(object o, int indent)
            {
                if (o != null)
                {
                    Type t = o.GetType();
                    Type[] gt = t.GetGenericArguments();                if (gt.Length == 0)
                    {
                        System.Console.WriteLine("value[" + t.Name + "] = " + o.ToString());
                        System.Console.WriteLine("--------------------------------------------");                    foreach (PropertyInfo pi in t.GetProperties())
                        {
                            object o2 = pi.GetValue(o, null);
                            if (o2 != null)
                            {
                                System.Console.WriteLine(GetIndent(indent) + pi.Name + "[" + pi.PropertyType.Name + "] = " + o2.ToString());
                            }
                        }
                    }
                    else
                    {
                        System.Type t2 = gt[0];                   
                        IList os = (IList)o;                    foreach (object o2 in os)
                        {
                            OutPutObject(o2, indent + 1);
                        }
                    }
                }
                else
                {
                    System.Console.WriteLine("null");
                }
            }这样也行,紫色阴影,3Q
      

  8.   

    问题解决了
    我把代码贴出来供大家参考
    public class TestManager
        {
            public static void OutPutObject(object o)
            {
                OutPutObject(o, 0);
            }        public static void OutPutObject(object o, int indent)
            {
                if (o != null)
                {
                    Type t = o.GetType();
                    System.Console.WriteLine(GetIndent(indent) + "value[" + t.Name + "] = " + o.ToString());
                    System.Console.WriteLine(GetIndent(indent) + "--------------------------------------------");
                    
                    Type[] gt = t.GetGenericArguments();                if (gt.Length == 0)
                    {
                        foreach (PropertyInfo pi in t.GetProperties())
                        {
                            object o2 = pi.GetValue(o, null);
                            if (o2 != null)
                            {
                                System.Console.WriteLine(GetIndent(indent) + pi.Name + "[" + pi.PropertyType.Name + "] = " + o2.ToString());
                            }
                        }
                    }
                    else
                    {
                        IList os = (IList)o;
                        System.Console.WriteLine(GetIndent(indent) + "Count:" + os.Count);                    foreach (object o2 in os)
                        {
                            OutPutObject(o2, indent + 1);
                        }
                    }
                }
                else
                {
                    System.Console.WriteLine("null");
                }
            }        public static string GetIndent(int indent)
            {
                StringBuilder sb = new StringBuilder();
                for (int index = 0; index < indent; index++)
                {
                    sb.Append("      ");
                }
                return sb.ToString();
            }
        }