0

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

Bjørn
  • 1,138
  • 2
  • 16
  • 47
  • You should call `.Invoke(null, objectArray)` instead of `.Invoke(objectArray)`. – Steven Jun 18 '13 at 10:31
  • Still says cannot be found... should the new object[1] have some magic in it? – Bjørn Jun 18 '13 at 10:42
  • Did you see this related question: http://stackoverflow.com/questions/2438065/c-sharp-reflection-how-can-i-invoke-a-method-with-an-out-parameter. – Steven Jun 18 '13 at 10:45
  • Yes, but the question is not about static methods.. and they did not use the GetStaticMethod on an assembly object.... – Bjørn Jun 18 '13 at 10:48

2 Answers2

0

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;
            }
        }
    }
S.N
  • 4,910
  • 5
  • 31
  • 51
  • Thanks for suggestion.. but if you read the comments this is not what I am looking for... I want to use the GetStaticMethod or GetStaticMethodWithArgs functions.... :) This is because I want to use the assembly object which is described in the question... – Bjørn Jun 18 '13 at 11:35
0

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

Bjørn
  • 1,138
  • 2
  • 16
  • 47