CS中的实用程序(-)Ilist 转换到datatable

datatable在某些方面要比Ilist操作起来方便很多,尤其是修改一些老程序时,客户端已经都绑定到了Dataset或者datatable,如果让他们重新绑定到Ilist会做大量的工作。
如果能转换到Datatable会方便很多

None.gif public   class  CADataConverter
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif
InBlock.gif        
private static Hashtable types = new Hashtable();
InBlock.gif
InBlock.gif        
private CADataConverter()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Converts an IList into a datatable.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="list">List of objects. First item can not be null</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static DataTable ToDataTable(IList list)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(list == null)
InBlock.gif                
throw new ArgumentNullException("list""List can not be null");
InBlock.gif
InBlock.gif            
if(list.Count == 0)
InBlock.gif                
throw new ArgumentOutOfRangeException("list""List can not be empty without supplying a type. Please use the other overload");
InBlock.gif
InBlock.gif            
object obj = list[0];
InBlock.gif            
if(obj == null)
InBlock.gif                
throw new ArgumentOutOfRangeException("list""First item in collection can not equal null");
InBlock.gif
InBlock.gif
InBlock.gif            
return ToDataTable(list,obj.GetType());
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Converts an IList into a datatable.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="list"></param>
InBlock.gif        
/// <param name="t">Since a type is supplied, the list can be null or empty</param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static DataTable ToDataTable(IList list, Type t)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataTable dt 
= types[t] as DataTable;
InBlock.gif            
if(dt == null)
InBlock.gif                dt 
= CreateShell(t);
InBlock.gif            
else
InBlock.gif                dt 
= dt.Clone();
InBlock.gif
InBlock.gif            
if(list == null || list.Count == 0)
InBlock.gif                
return dt;
InBlock.gif
InBlock.gif            
foreach(object item in list)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(item != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    DataRow dr 
= dt.NewRow();
InBlock.gif                    
foreach(DataColumn dc in dt.Columns)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            dr[dc.ColumnName] 
= t.GetProperty(dc.ColumnName).GetValue(item,null);
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockStart.gifContractedSubBlock.gif                        
catchdot.gif{}
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    dt.Rows.Add(dr);
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return dt;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Creates a datatable with datacolumns
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="t"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        protected static DataTable CreateShell(Type t)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            DataTable dt 
= new DataTable(t.Name);
InBlock.gif            PropertyInfo[] pia 
= t.GetProperties();
InBlock.gif            
foreach (PropertyInfo pi in pia)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(pi.CanRead)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
string st = pi.PropertyType.ToString();
InBlock.gif                    
switch(st)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
case "System.String":
InBlock.gif                        
case "System.Int32":
InBlock.gif                        
case "System.Boolean":
InBlock.gif                        
case "System.Double":
InBlock.gif                        
case "System.Guid":
InBlock.gif                            dt.Columns.Add(pi.Name,pi.PropertyType);
InBlock.gif                            
break;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            types[t] 
= dt;
InBlock.gif
InBlock.gif            
return dt;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

通过反射取到Type的信息,type的属性被做为列名,CreatShell 实现
类型的属性值作为行。TODatatable实现
调用:
DataTable dt = CADataConverter.ToDataTable(categories,typeof(PostCategory));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值