1. 程式人生 > >將實體轉換為Hashtable

將實體轉換為Hashtable

1、將實體轉換為Hashtable,用於將實體引數處理為hashtable,方便sql引數傳遞

 1      /// <summary>
 2         /// 將實體轉換為Hashtable
 3         /// </summary>
 4         /// <typeparam name="T">實體型別</typeparam>
 5         /// <param name="obj">實體例項</param>
 6         /// <returns>Hashtable</returns>
7 public static Hashtable ConvertEntityToHashtable<T>(T obj) 8 { 9 Hashtable hst = new Hashtable(); 10 try 11 { 12 Type type = obj.GetType(); 13 foreach (PropertyInfo pi in type.GetProperties()) 14 {
15 PropertyInfo property = type.GetProperty(pi.Name); 16 hst.Add(pi.Name, property.GetValue(obj, null)); 17 } 18 return hst; 19 } 20 catch (Exception ex) 21 { 22 System.Console.Out.WriteLine(ex.Message);
23 return null; 24 } 25 }