将实体转换为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         }

猜你喜欢

转载自www.cnblogs.com/MirZhai/p/10059452.html