2

I have the following method delared by Gl:

public static void Get(int pname, out int @params)

I'm trying to get it using reflection in the following way:

MethodInfo mGetMethod = typeof(Gl).GetMethod("Get",
                                             BindingFlags.Public|BindingFlags.Static,
                                             null, 
                                             new Type[] 
                                             { 
                                                 typeof(Int32), 
                                                 typeof(Int32) 
                                             }, 
                                             null);

But I have no success. Why?

Is it because the out keyword?

Agustin Meriles
  • 4,866
  • 3
  • 29
  • 44
Luca
  • 11,646
  • 11
  • 70
  • 125
  • are you using .NET 4? If so you can use the dynamic keyword to create a reflected object, then call the method directly. That way you don't need the MethodInfo lookup. Downside is, if you get it wrong, you get a runtime exception – ghostJago Aug 25 '11 at 17:58

4 Answers4

7

Use typeof(Int32).MakeByRefType() for your second parameter. I.e.:

MethodInfo mGetMethod = typeof(Gl).GetMethod("Get", bindingFlags.Public|BindingFlags.Static, null, new Type[] { typeof(Int32), typeof(Int32).MakeByRefType() }, null);
Isaac Overacker
  • 1,385
  • 10
  • 22
  • Found it: http://stackoverflow.com/questions/2438065/c-reflection-how-can-i-invoke-a-method-with-an-out-parameter – Luca Aug 25 '11 at 17:57
1

If you need to specify the specific overload for the method then definitely go with what @Isaac Overacker said. Otherwise just don't specify the parameters:

MethodInfo mGetMethod = typeof(Gl).GetMethod("Get", BindingFlags.Public | BindingFlags.Static);
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
1

The out keyword passes the parameter by reference, which is probably your problem. You will need to flag it as a reference type since C# allows you to overload methods with a byValue and byReference parameter.

kmkemp
  • 1,656
  • 3
  • 18
  • 21
0

How about trying something like this instead:

MethodInfo method = this.GetType().GetMethod("Get");
if (method != null)
{
    method.Invoke(this, new object[] { "Arg1", "Arg2", "Arg3" });
}
James Johnson
  • 45,496
  • 8
  • 73
  • 110