通过反射机制得到IList

代码
public   static  IList  < T >  FillList  < T > (System.Data.IDataReader reader)
        {
            IList 
< T >  lst =   new  List  < T > ();
            
while  (reader.Read())
            {
                  T RowInstance 
=  Activator.CreateInstance  < T > ();
                
foreach  (PropertyInfo Property  in   typeof (T).GetProperties())
                {
                    
foreach  (BindingFieldAttribute FieldAttr  in     Property.GetCustomAttributes( typeof (BindingFieldAttribute),  true ))
                  {
                  
try
                    {
          
int  Ordinal  =  reader.GetOrdinal(FieldAttr.FieldName);
                          
if  (reader.GetValue(Ordinal)  !=  DBNull.Value)
                          {
                                Property.SetValue(RowInstance, Convert.ChangeType(reader.GetValue(Ordinal), Property.PropertyType), 
null );
                            }
                        }
                        
catch
                        {
                            
break ;
                        }
                    }
                }
                lst.Add(RowInstance);
            }
            
return  lst;
        } 

泛型的优势 体现的淋漓尽致

dataread读数据的时候 老是写一大堆的dr["s"]=s.soo 诸如此类的东东 烦了  所以 网上找了一下 弄来了一些东西 看看

你可能感兴趣的:(list)