编辑:我试图在里面运行它的类/方法是静态的,因此我无法将它传递给 generic.Invoke
我有一个静态数据访问类,用于自动解析来自各种来源的数据。当我遇到问题时,我开始重新考虑它。我想通过反射将类型传递给通用方法,(该方法然后解析类型并返回带有值的类型)我的代码目前看起来像
Type type1 = typeof( T );
var item = (T)Activator.CreateInstance( typeof( T ), new object[] { } );
foreach (PropertyInfo info in type1.GetProperties())
{
Type dataType = info.PropertyType;
Type dataType = info.PropertyType;
MethodInfo method = typeof( DataReader ).GetMethod( "Read" );
MethodInfo generic = method.MakeGenericMethod( dataType );
//The next line is causing and error as it expects a 'this' to be passed to it
//but i cannot as i'm inside a static class
generic.Invoke( this, info.Name, reader );
info.SetValue(item,DataReader.Read<dataType>(info.Name, reader ) , null);
}