我有一个方法,输入类名,通过反射去获取该类所有实体的数据,
但数据都放在object中返回,
private object GetEntities(string fullClassName)
        {
            Type bllType = this.GetClassType(fullClassName);
            Object result = null;
            Object obj = Activator.CreateInstance(bllType);
            result = bllType.InvokeMember("GetEntities", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, obj,null);
            return result;
        }
。返回来后不知道怎么把此object转换成dataTable来使用

解决方案 »

  1.   

    如果确定object是table,可以强制转换,if(obj!= null)
    DateTable tb = (DateTable)0
      

  2.   

    DataTable dt= (DataTable)GetEntities("classname");
      

  3.   

    object里面放的是list<classFullName对应类>这个怎么办!
      

  4.   

    一样啊,类似List<string>  ls= (List<string> )GetEntities("classname");
      

  5.   

    public static DataTable ToDataTable<TResult>(this IEnumerable<TResult> value) where TResult : class
      {
      //创建属性的集合
      List<PropertyInfo> pList = new List<PropertyInfo>();
      Type type = typeof(TResult);
      DataTable dt = new DataTable();
      Array.ForEach<PropertyInfo>(type.GetProperties(), p => { pList.Add(p);dt.Columns.Add(p.Name, p.PropertyType); });   
      foreach (var item in value)
      {
      //创建一个DataRow实例
      DataRow row = dt.NewRow();
      pList.ForEach(p => row[p.Name] = p.GetValue(item, null));
      dt.Rows.Add(row);
      }
      return dt;
      }
      }