類轉化爲datatable

DataTable dt = new DataTable();
            _EvaluateModel ee = new _EvaluateModel();
            try
            {
                Type cctype = ee.GetType();


                foreach (PropertyInfo pi in cctype.GetProperties())
                {
                    string Name = "";
                    Name = pi.Name;
                    if (!string.IsNullOrEmpty(Name))
                    {
                        dt.Columns.Add(Name, pi.PropertyType);
                    }
                }


                if (dt.Columns.Count > 0)
                {
                    for (int i = 0; i < mylist.Count; i++)
                    {
                        _EvaluateModel Item = mylist[i];


                        DataRow row = dt.NewRow();
                        Type type = Item.GetType();


                        foreach (PropertyInfo info in type.GetProperties())
                        {
                            row[info.Name] = info.GetValue(Item, null);
                        }


                        dt.Rows.Add(row);
                    }
                }
            }
            catch (Exception ex) { }
            return dt;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章