首页 > 其他 > 详细

使用反射给泛型类赋值

时间:2014-02-10 16:33:17      阅读:348      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
public static List<T> ToList<T>( this OracleDataReader reader )
        {
            List<T> list = new List<T>();
            Type type = typeof( T );
            while( reader.Read() )
            {
                T t = Activator.CreateInstance<T>();
                int filedCount = reader.FieldCount;
                for( int i = 0 ; i < filedCount ; i ++ )
                {
                    var property = type.GetProperty( reader.GetName( i ) );
                    object value = reader[ i ];
                    if( property != null && property.CanWrite && !( value is DBNull ) )
                    {
                        property.SetValue( t , value , null );
                    }
                }
                list.Add( t );
            }
            return list;
        }
bubuko.com,布布扣

使用反射给泛型类赋值

原文:http://www.cnblogs.com/Shoring/p/3542341.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!