的扩展,这个问题通过静态反射信息静态泛型方法
我试着去建立一个通用型读写器,因为我使用类做了很多的数据访问,我想创建一个非常通用的方法,可以让没有太多的代码读取数据。
,做阅读的大部分代码的部分看起来像这样
public static T Read<T>(string field,IDataRecord data )
{
Type type1 = typeof (T);
try
{
if (type1 == typeof( String ))
{
return (T)Convert.ChangeType( readString( data[field].ToString() ), typeof( T ) );
}
if (type1 == typeof( int? ))
{
return (T)Convert.ChangeType( readIntN( data[field].ToString() ), typeof( T ) );
}
if (type1 == typeof( Guid? ))
{
return (T)Convert.ChangeType( readGuidN( data[field].ToString() ), typeof( T ) );
}
if (type1 == typeof( double? ))
{
return (T)Convert.ChangeType( readDoubleN( data[field].ToString() ), typeof( T ) );
}
if (type1 == typeof( decimal? ))
{
var res = readDecimalN(data[field].ToString());
return (T)Convert.ChangeType( res, typeof( T ) );
}
if (type1 == typeof( float? ))
{
return (T)Convert.ChangeType( readFloatN( data[field].ToString() ), typeof( T ) );
}
if (type1 == typeof( bool? ))
{
return (T)Convert.ChangeType( readBoolN( data[field].ToString() ), typeof( T ) );
}
if (type1 == typeof( DateTime? ))
{
return (T)Convert.ChangeType( readDatetimeN( data[field].ToString() ), typeof( T ) );
}
if (type1 == typeof( int ))
{
return (T)Convert.ChangeType( readInt( data[field].ToString() ), typeof( T ) );
}
if (type1 == typeof( long? ))
{
return (T)Convert.ChangeType( readLongN( data[field].ToString() ), typeof( T ) );
}
if (type1 == typeof( long ))
{
return (T)Convert.ChangeType( readLong( data[field].ToString() ), typeof( T ) );
}
if (type1 == typeof( Guid ))
{
return (T)Convert.ChangeType(readGuid( data[field].ToString() ), typeof( T ) );
}
if (type1 == typeof( double ))
{
return (T)Convert.ChangeType( readDouble( data[field].ToString() ), typeof( T ) );
}
if (type1 == typeof( decimal ))
{
return (T)Convert.ChangeType( readDecimal( data[field].ToString() ), typeof( T ) );
}
if (type1 == typeof( float ) || type1 == typeof( Single ))
{
return (T)Convert.ChangeType( readFloat( data[field].ToString() ), typeof( T ) );
}
if (type1 == typeof( bool ))
{
return (T)Convert.ChangeType( readBool( data[field].ToString() ), typeof( T ) );
}
if (type1 == typeof( DateTime ))
{
return (T)Convert.ChangeType( readDatetime( data[field].ToString() ), typeof( T ) );
}
}
catch (Exception)
{
throw;
}
throw new Exception(String.Format("Data Type Not Supported: {0}", type1));
}
然而,这是引发无效转换异常。
该方法的readXXX正在罚款的问题是在每个return语句中存在的
我一直在使用也尝试
public static T SafeConvert<T>(string s, T defaultValue)
{
if ( string.IsNullOrEmpty(s) )
return defaultValue;
return (T)Convert.ChangeType(s, typeof(T));
}
但它仍然失败
编辑:
该方法被称为通过
private static List<T> GetItems<T>(IDataReader reader)
{
var items = new List<T>();
while (reader.Read())
{
Type type1 = typeof (T);
var item = (T) Activator.CreateInstance(typeof (T), new object[] {});
foreach (PropertyInfo info in type1.GetProperties())
{
int written = 0;
if (info.CanWrite)
{
#region
try
{
Type dataType = info.PropertyType;
MethodInfo method = typeof (DataReader).GetMethod("Read",BindingFlags.Static | BindingFlags.Public);
MethodInfo generic = method.MakeGenericMethod(dataType);
var t = generic.Invoke(null, new object[] {info.Name, reader});
info.SetValue( item, t );
更多...
人们似乎是问什么我用这个,最终它可以让我在一行中创建一个IEnumerable是从任意源读取无论是通过传递一个文件位置,或者SQL查询即CSV或SQL
//returns Ienumerable<MyClass>
var list = Reader.ReadSql<MyClass>(DataBases.Test,"select * from TestTable where someAttribute = 1");
// also returns Ienumerable MyClass
var list2 = Readre.ReadCsv<MyClass>(@"c:\file1.csv",",");
我现在运行,但它要求如果我希望重构是到一个通用的Read方法各实施的dataType == typeof运算(字符串)一长串的repetiiton但我有使用转换的麻烦