MethodInfo.Invoke(对象引用未设置为对象的实例。)在抽象类

时间:2015-08-18 12:19:46

标签: c# reflection abstract

我试图用反射复制一个功能但最终得到

  

createFormatMethod.Invoke(typDbFormatClass,null)'抛出类型' System.Reflection.TargetInvocationException' object {System.Reflection.TargetInvocationException}

内部异常显示Object reference not set to an instance of an object. System.NullReferenceException

InnerException null

堆栈跟踪

at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[]   arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
at AB.INIT.stringToValue(String valueInUnits, String dimension, String sourceUnit, String destinationUnit)

我试图在这里访问抽象类的静态方法。以下是完美运行的直接参考代码。

Binit.Core.Database.DbFormat format;
format = Binit.Core.Database.DbFormat.Create();
format.Dimension = DbDoubleDimension.GetDimension(
                   (DbDimension)Enum.Parse(typeof(DbDimension), dimension));
format.Units = DbDoubleUnits.GetUnits(
                (DbUnits)Enum.Parse(typeof(DbUnits), destinationUnit));

反射代码失败并显示targetinvocationexception

Assembly CoreDatabaseAssembly = 
         Assembly.LoadFrom(Path.Combine(APath, @"Binit.Core.Database.dll"));

Type typDbFormatClass = CoreDatabaseAssembly.GetType("Binit.Core.Database.DbFormat");

MethodInfo createFormatMethod = typDbFormatClass.GetMethod("Create", 
           BindingFlags.Static | BindingFlags.Public, null, new Type[] { }, null);

object objDbFormat = createFormatMethod.Invoke(typDbFormatClass, null);

有人可以帮我理解调用方法

我可能做错了什么

0 个答案:

没有答案