MethodInfo.Invoke and reading ref parameters

  • Thread starter Thread starter ian
  • Start date Start date
I

ian

Hi,

I can't find a solution to this, so I've brought it to the experts.
Using reflection I can get a MethodInfo object pointing at an assembly's
method.
Where I have a MethodInfo object pointing at a function, say func(int
p1, ref int p2), how do I read p2 after a successful invocation?
I've tried interogating the parameterinfo objects on the method, I've
seen some hints about overriding something or casting some other object
type to an array of objects, but no concrete examples, not even some
sand ones!

Thank you
 
ian said:
I can't find a solution to this, so I've brought it to the experts.
Using reflection I can get a MethodInfo object pointing at an assembly's
method.
Where I have a MethodInfo object pointing at a function, say func(int
p1, ref int p2), how do I read p2 after a successful invocation?

The object array you pass to Invoke() gets modified, and the value
returned is put in the correct location in the array.
I've tried interogating the parameterinfo objects on the method, I've
seen some hints about overriding something or casting some other object
type to an array of objects, but no concrete examples, not even some
sand ones!

---8<---
using System;
using System.Reflection;

class App
{
static void Main()
{
MethodInfo doubleInt = typeof(App).GetMethod("DoubleInt",
BindingFlags.NonPublic | BindingFlags.Static);
object[] args = { 21 };
doubleInt.Invoke(null, args);
Console.WriteLine(args[0]);
}

static void DoubleInt(ref int x)
{
x = x * 2;
}
}
--->8---

-- Barry
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top