Pergunta

I have a simple static method that does not contain out parameters, returns anything or takes any arguments. I run it this way:

Assembly assembly = ResourceConfig.GetAssembly("IntegrationServices");
assembly.GetStaticMethod("Current.IntegrationServices.SomeIntegration.SomeMethod").Invoke();

Which seems to be running ok...

Next i have a static method that returns one out parameter (which is string), and returns a boolean value. I want to run this, but cant figure out what i am doing wrong. This is what i have so far:

var objectArray = new object[1];
(bool)assembly.GetStaticMethod("Current.IntegrationServices.SomeIntegration.ReturningMethod").Invoke(objectArray)

From what i have understood i should be able to access objectArray[0] and get my out value.. but when trying to run this code i get the error:

Method Current.IntegrationServices.SomeIntegration.ReturningMethod() cannot be found.

And i assure you the method does indeed exist... :)

Calling this method without reflection would happen like this:

string s;
bool value = Current.IntegrationServices.SomeIntegration.ReturningMethod(out s);

Any suggestions on how to make it run with the GetStaticMethod and Invoke?

EDIT: I just found a method called GetStaticMethodWithArgs(this Assembly obj, string methodName, params Type[] list):MethodDelegate how would i use this?

EDIT 2: I have now been able to run a static method with arguments and it happens like this:

Assembly assembly = ResourceConfig.GetAssembly("IntegrationServices");
var staticMethodWithArgs = assembly.GetStaticMethodWithArgs("Current.IntegrationServices.SomeIntegration.ReturningMethod", typeof(string), typeof(string));
staticMethodWithArgs.Invoke(InputUsername.Text, InputPassword.Text)

Still cant use method with out parameter... suggestions are appriciated

Foi útil?

Solução 2

After much mayhem and testing i figured it out.... It was all dandy and fine if i just used the proper type of the variable... It had to be String&.. The way i got this was by the line:

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

Further as i tried this the code looked like this:

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);

Which in turn led me to invoke the MethodInfo, and drop the GetStaticMethodWithArgs concept... If anyone knows how to get the type String& in this manner, without crashing: typeof(String&) I would be greatful :)

Outras dicas

You need to work with the BindingFlags and probably this what you are missing. Have a look at this MSDN link. In order to demonstrate, following code block reflect a static method which return bool and modify the out argument.

 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;
            }
        }
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top