用.net泛型写简单对象持久化Hibernate.net

 

        public static void SaveObject(System.Object o, System.Collections.Generic.List<System.String> sl)
        
{
            System.Data.DataTable dt 
= new DataTable(o.GetType().Name);
            System.Collections.Generic.List
<System.Data.DataColumn> keys = new List<DataColumn>();
            
foreach (System.Reflection.PropertyInfo pi in o.GetType().GetProperties())
            
{
                
if (pi.CanRead && pi.CanWrite)
                
{
                    System.Data.DataColumn dc 
= dt.Columns.Add(pi.Name, pi.PropertyType);
                    
if (sl.Contains(dc.ColumnName))
                    
{
                        keys.Add(dc);
                    }

                }

            }

            
if (keys.Count > 0)
                dt.PrimaryKey 
= keys.ToArray();
            System.Data.DataRow dr 
= dt.NewRow();
            
foreach (System.Reflection.PropertyInfo pi in o.GetType().GetProperties())
            
{
                
if (pi.CanRead && pi.CanWrite)
                
{
                    dr[pi.Name] 
= pi.GetValue(o, null);
                }

            }

            dt.Rows.Add(dr);
            dt.AcceptChanges();
            dt.WriteXml(dt.TableName 
+ ".xml", XmlWriteMode.WriteSchema);
        }
posted @ 2008-04-04 22:41  王伟晔  阅读(1483)  评论(0编辑  收藏  举报