我试图通过传递包含函数名称的字符串来调用函数。
下面是我的代码,这两种代码都不起作用。问题是我的MethodInfo变量mi总是空的。
在调试中,我使用GetMethods方法查看列出的所有函数。我试图调用的函数列出了,我已经检查了拼写。我做错了什么?
object[] paraArray = new object[] { corpAct, secEvents };
MethodInfo mi = this.GetType().GetMethod(corpAct.DelegateName);
mi.Invoke(this, paraArray);第二次尝试
Type[] paraTypes = new Type[] { typeof(IHoldingLog), typeof(SecurityEvent) };
object[] paraArray = new object[] { corpAct, secEvents };
MethodInfo mi = this.GetType().GetMethod(corpAct.DelegateName, paraTypes);
mi.Invoke(this, paraArray);它试图调用的函数,
void myFunction(IHoldingLog log, SecurityEvent sec)更新
我只是尝试使用下面的行,但仍然将mi作为空。
MethodInfo mi = this.GetType().GetMethod(corpAct.DelegateName, BindingFlags.NonPublic);发布于 2016-11-04 08:53:52
默认情况下,GetMethod只返回public实例或静态方法。因此,如果您不指定BindingFlags,默认情况下它将返回到BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public。
使用下面的调用变量也包括非公共方法。
this.GetType().GetMethod("myMethod", BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)如果您的方法有重载,那么您可以使用GetMethod重载,它允许您指定参数类型来标识您希望目标的重载(如第二个示例中所示)。如果您的方法没有重载,那么不需要指定参数类型。
https://stackoverflow.com/questions/40418423
复制相似问题