使用反射给泛型类赋值

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;
        }
原文地址:https://www.cnblogs.com/Shoring/p/3542341.html