2009-02-13 127 views
6
public static IList<T> ConvertTo<T>(DataTable table) 
    { 
     if (table == null) 
     { 
      return null; 
     } 

     List<DataRow> rows = new List<DataRow>(); 

     foreach (DataRow row in table.Rows) 
     { 
      rows.Add(row); 
     } 

     return ConvertTo<T>(rows); 
    } 

    public static T ConvertItem<T>(DataTable table) 
    { 
     T obj = default(T); 
     if (table != null && table.Rows.Count > 0) 
     { 
      obj = CreateItem<T>(table.Rows[0]); 
     } 
     return obj; 
    } 


    public static T CreateItem<T>(DataRow row) 
    { 
     T obj = default(T); 
     if (row != null) 
     { 
      obj = Activator.CreateInstance<T>(); 
      Type entityType = typeof(T); 
      PropertyInfo[] properties = entityType.GetProperties(); 

      for (int i = 0; i < properties.Length; i++) 
      { 
       object[] customAttributes = properties[i].GetCustomAttributes(typeof(ColumnAttributes), false); 
       ColumnAttributes dataField = null; 
       if (null != customAttributes && customAttributes.Length > 0 && null != (dataField = customAttributes[0] as ColumnAttributes)) 
       { 
        if (row.Table.Columns.Contains(dataField.FieldName) && !row[dataField.FieldName].GetType().FullName.Equals("System.DBNull")) 
        { 
         properties[i].SetValue(obj, row[dataField.FieldName], null); 
        } 
       } 
      } 
     } 
     return obj; 
    } 

這就是我們現在唯一能想到的,就是我們必須做一些我們需要的東西垃圾收集我們自己?將DataTable轉換爲泛型列表?

想法?

爲什麼我們認爲有可能是泄漏?:

我們越來越內存不足的錯誤。如果頁面不需要業務邏輯來使用這種類型的轉換,那麼II6過程不會增長,但是當我們點擊一​​個使用它的頁面時,它會增長。

我們目前正在讓螞蟻探查器給我們更多的細節。

+0

你有什麼證據泄漏的? – 2009-02-13 09:32:21

+0

準確的問題在哪裏? – 2009-02-13 09:35:49

回答

9

這不會是一個實際的泄漏,但它可能會不必要地強調的東西...

有多少行是你的工作了嗎? 請注意,反射是一個痛苦,並且每次調用GetCustomAttributes之類的東西都可以返回一個新數組(因此您希望這樣做一次,而不是每個屬性每行一次)。

就我個人而言,我會預先構建我打算做的工作......像下面這樣的東西。

請注意,如果我這樣做很多,我會切換到HyperDescriptor,或者如果.NET 3.5是一個選項,也許是編譯的表達式。由於DataTable不是強類型,HyperDescriptor會後下方的合乎邏輯的下一步(性能)...

sealed class Tuple<T1, T2> 
{ 
    public Tuple() {} 
    public Tuple(T1 value1, T2 value2) {Value1 = value1; Value2 = value2;} 
    public T1 Value1 {get;set;} 
    public T2 Value2 {get;set;} 
} 
public static List<T> Convert<T>(DataTable table) 
    where T : class, new() 
{ 
    List<Tuple<DataColumn, PropertyInfo>> map = 
     new List<Tuple<DataColumn,PropertyInfo>>(); 

    foreach(PropertyInfo pi in typeof(T).GetProperties()) 
    { 
     ColumnAttribute col = (ColumnAttribute) 
      Attribute.GetCustomAttribute(pi, typeof(ColumnAttribute)); 
     if(col == null) continue; 
     if(table.Columns.Contains(col.FieldName)) 
     { 
      map.Add(new Tuple<DataColumn,PropertyInfo>(
       table.Columns[col.FieldName], pi)); 
     } 
    } 

    List<T> list = new List<T>(table.Rows.Count); 
    foreach(DataRow row in table.Rows) 
    { 
     if(row == null) 
     { 
      list.Add(null); 
      continue; 
     } 
     T item = new T(); 
     foreach(Tuple<DataColumn,PropertyInfo> pair in map) { 
      object value = row[pair.Value1]; 
      if(value is DBNull) value = null; 
      pair.Value2.SetValue(item, value, null); 
     } 
     list.Add(item); 
    } 
    return list;   
}