【发布时间】:2013-06-18 10:26:11
【问题描述】:
我有一个简单的静态方法,它不包含输出参数、返回任何内容或接受任何参数。我是这样运行的:
Assembly assembly = ResourceConfig.GetAssembly("IntegrationServices");
assembly.GetStaticMethod("Current.IntegrationServices.SomeIntegration.SomeMethod").Invoke();
这似乎运行正常...
接下来我有一个静态方法,它返回一个输出参数(字符串),并返回一个布尔值。我想运行它,但无法弄清楚我做错了什么。这是我到目前为止所拥有的:
var objectArray = new object[1];
(bool)assembly.GetStaticMethod("Current.IntegrationServices.SomeIntegration.ReturningMethod").Invoke(objectArray)
据我了解,我应该能够访问 objectArray[0] 并获得我的输出值.. 但是在尝试运行此代码时出现错误:
Method Current.IntegrationServices.SomeIntegration.ReturningMethod() cannot be found.
我向你保证,该方法确实存在...... :)
在没有反射的情况下调用这个方法会发生这样的情况:
string s;
bool value = Current.IntegrationServices.SomeIntegration.ReturningMethod(out s);
关于如何使用 GetStaticMethod 和 Invoke 运行它有什么建议吗?
编辑: 我刚刚找到了一个名为 GetStaticMethodWithArgs(this Assembly obj, string methodName, params Type[] list):MethodDelegate 我将如何使用它的方法?
编辑 2: 我现在已经能够运行带有参数的静态方法,它的发生是这样的:
Assembly assembly = ResourceConfig.GetAssembly("IntegrationServices");
var staticMethodWithArgs = assembly.GetStaticMethodWithArgs("Current.IntegrationServices.SomeIntegration.ReturningMethod", typeof(string), typeof(string));
staticMethodWithArgs.Invoke(InputUsername.Text, InputPassword.Text)
仍然不能使用没有参数的方法...建议
【问题讨论】:
-
你应该打电话给
.Invoke(null, objectArray)而不是.Invoke(objectArray)。 -
还是说找不到……新对象[1]应该有什么魔力吧?
-
你看到这个相关的问题了吗:stackoverflow.com/questions/2438065/….
-
是的,但问题不在于静态方法……而且他们没有在程序集对象上使用 GetStaticMethod……
标签: c# .net reflection static out-parameters