【问题标题】:Run static method with out parameter using reflection使用反射运行不带参数的静态方法
【发布时间】: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


【解决方案1】:

您需要使用BindingFlags,这可能是您所缺少的。看看这个MSDN 链接。为了演示,下面的代码块反映了return bool 并修改out 参数的静态方法。

 using System;
    using System.Reflection;
    namespace ConsoleApplication1
    {
        public class StaticInvoke
        {
            private static void Main()
            {
                MethodInfo info = typeof(StaticInvoke).GetMethod("SampleMethod", BindingFlags.Public | BindingFlags.Static);
                var input = new object[] {"inputValue"};
                var value = info.Invoke(null, input);
                Console.WriteLine(value);
                Console.WriteLine(input[0]);
                Console.ReadLine();
            }

            public static bool SampleMethod(out string input)
            {
                input = "modified val";
                Console.WriteLine("I am executing");
                return true;
            }
        }
    }

【讨论】:

  • 感谢您的建议.. 但是如果您阅读了 cmets,这不是我想要的...我想使用 GetStaticMethod 或 GetStaticMethodWithArgs 函数.... :) 这是因为我想要使用问题中描述的程序集对象...
【解决方案2】:

经过多次混乱和测试,我想通了...... 如果我只使用正确的变量类型,那就太好了……它必须是 String&……我得到这个的方式是:

methodInfo.GetParameters()[0].ParameterType.UnderlyingSystemType

当我进一步尝试时,代码看起来像这样:

Assembly assembly = ResourceConfig.GetAssembly("IntegrationServices");
BindingFlags bindingFlags = BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod;
MethodInfo methodInfo = assembly.GetType("Current.IntegrationServices.SomeIntegration").GetMethod("GetAbaxUserToken", bindingFlags);

var staticMethodWithArgs = assembly.GetStaticMethodWithArgs("Current.IntegrationServices.SomeIntegration.ReturningMethod", methodInfo.GetParameters()[0].ParameterType.UnderlyingSystemType);

这反过来又导致我调用 MethodInfo,并放弃 GetStaticMethodWithArgs 概念...如果有人知道如何以这种方式获取类型 String& 而不会崩溃:typeof(String&) 我会很高兴:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多