通过反射填充泛型集合List的静态方法

本文介绍了一种通过反射机制从DataReader将数据填充到数据实体泛型集合的方法,并提供了一个具体的实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

呃```花了一晚上时间,终于搞出来了如何通过反射,从DataReader将数据填充到数据实体泛型集合的静态方法.

//Kchen.Core.BaseBusinessObject为通用数据实体类,此处仅为限定T所继承的类型
None.gif        public static IList<T> FillDataListGeneric<T>(System.Data.IDataReader reader) where T : Kchen.Core.BaseBusinessObject
ExpandedBlockStart.gifContractedBlock.gif
        dot.gif{
InBlock.gif            
//实例化一个List<>泛型集合
InBlock.gif
            IList<T> DataList = new List<T>();
InBlock.gif            
while (reader.Read())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//由于是是未知的类型,所以必须通过Activator.CreateInstance<T>()方法来依据T的类型动态创建数据实体对象
InBlock.gif
                T RowInstance = Activator.CreateInstance<T>();
InBlock.gif                
//通过反射取得对象所有的Property
InBlock.gif
                foreach (PropertyInfo Property in typeof(T).GetProperties())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
//BindingFieldAttribute为自定义的Attribute,用于与数据库字段进行绑定
InBlock.gif
                    foreach (BindingFieldAttribute FieldAttr in Property.GetCustomAttributes(typeof(BindingFieldAttribute), true))
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
//取得当前数据库字段的顺序
InBlock.gif
                            int Ordinal = reader.GetOrdinal(FieldAttr.FieldName);
InBlock.gif                            
if (reader.GetValue(Ordinal) != DBNull.Value)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
dot.gif{
InBlock.gif                                
//将DataReader读取出来的数据填充到对象实体的属性里
InBlock.gif
                                Property.SetValue(RowInstance, Convert.ChangeType(reader.GetValue(Ordinal), Property.PropertyType), null);
ExpandedSubBlockEnd.gif                            }

ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            
break;
ExpandedSubBlockEnd.gif                        }

ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

InBlock.gif                
//将数据实体对象add到泛型集合中
InBlock.gif
                DataList.Add(RowInstance);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return DataList;
ExpandedBlockEnd.gif        }

调用的时候使用如下代码

None.gif            //伪代码 OleDbDataReader _ds = 创建一个OleDbDataReader 
None.gif
            IList<Product> _result = Kchen.Utilities.FillDataListGeneric<Product>(_ds);


此静态方法通过一个实体类型和DateReader,快速的将数据填充到数据实体泛型集合中.

 

转载于:https://www.cnblogs.com/kchen/archive/2006/10/31/545011.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值