C# 使用反射运行带out参数的静态方法

C# 使用反射运行带out参数的静态方法,c#,.net,reflection,static,out-parameters,C#,.net,Reflection,Static,Out Parameters,我有一个简单的静态方法,它不包含out参数、返回任何内容或接受任何参数。我是这样运行的: Assembly assembly = ResourceConfig.GetAssembly("IntegrationServices"); assembly.GetStaticMethod("Current.IntegrationServices.SomeIntegration.SomeMethod").Invoke(); 看起来运行正常 接下来,我有一个静态方法,它返回一个out参数(即string)

我有一个简单的静态方法,它不包含out参数、返回任何内容或接受任何参数。我是这样运行的:

Assembly assembly = ResourceConfig.GetAssembly("IntegrationServices");
assembly.GetStaticMethod("Current.IntegrationServices.SomeIntegration.SomeMethod").Invoke();
看起来运行正常

接下来,我有一个静态方法,它返回一个out参数(即string),并返回一个布尔值。我想运行这个,但不知道我做错了什么。这就是我到目前为止所做的:

var objectArray = new object[1];
(bool)assembly.GetStaticMethod("Current.IntegrationServices.SomeIntegration.ReturningMethod").Invoke(objectArray)
据我所知,我应该能够访问objectArray[0]并获得我的out值。。但是当尝试运行此代码时,我得到了错误:

Method Current.IntegrationServices.SomeIntegration.ReturningMethod() cannot be found.
我向你保证,这种方法确实存在……:)

在不进行反射的情况下调用此方法的情况如下:

string s;
bool value = Current.IntegrationServices.SomeIntegration.ReturningMethod(out s);
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);
关于如何使用GetStaticMethod和Invoke运行它,有什么建议吗

编辑: 我刚刚找到一个名为GetStaticMethodWithArgs的方法(这个程序集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)

仍然无法使用没有参数的方法。。。您需要使用
BindingFlags
,这可能是您所缺少的。看看这个链接。为了演示,下面的代码块反映了一个静态方法,
返回
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;
            }
        }
    }

经过大量的混乱和测试,我发现了。。。。 如果我只使用正确类型的变量,这一切都很好。。。它必须是字符串&。。我得到这个的方式是这样的:

methodInfo.GetParameters()[0].ParameterType.UnderlyingSystemType
当我进一步尝试时,代码如下所示:

string s;
bool value = Current.IntegrationServices.SomeIntegration.ReturningMethod(out s);
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概念。。。如果有人知道如何获取类型字符串&以这种方式,而不会崩溃:typeof(String&)I将非常有用:)

您应该调用
。Invoke(null,objectArray)
而不是
。Invoke(objectArray)
。仍然说找不到。。。新对象[1]是否应该具有某种魔力?您是否看到此相关问题:。是的,但问题不是关于静态方法。。而且他们没有在程序集对象上使用GetStaticMethod…谢谢你的建议。。但是如果你读了评论,这不是我想要的。。。我想使用GetStaticMethod或GetStaticMethodWithArgs函数……)这是因为我想使用问题中描述的assembly对象。。。