reflection invoke with output parameters

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, I´m triying to invoke a member using reflection, but this member requiere
two parameters, the first patrameter is a string and the second is a
reference to a dataset (output parameter). The parameters has to be passed
in an array of objects.

I´ve tried something like this:

object[] parameters = new object[2];

parameters [0] ="text";

DataSet DS=new DadaSet();
parameters [1]= DS;

Myclass.InvokeMember("MyMethod",BindingFlags.InvokeMethod,null,
obj,parameters );

But it does´nt work because the second item of the array (parameters[1])
should be a refference to a dataset.... How can I solve this problem?
Thanks
 
Hi Gus,

try this sample:

using System;
using System.Collections;
using System.Reflection;

public class MyClass
{
public class TestClass {
public void MyMethod(out int x) {
x = 42;
}
}

public static void Main() {
TestClass testClass = new TestClass();
MethodInfo methodInfo = typeof(TestClass).GetMethod("MyMethod",
new Type[] { Type.GetType("System.Int32&") });

object[] parameters = new object[1];
methodInfo.Invoke(testClass, parameters);
Console.WriteLine(parameters[0]);
Console.ReadLine();
}
}



Oliver Sturm
 
Gus,

You will have to use the overload of InvokeMember that takes an array of
ParameterModifier instances. This will tell reflection to pay attention to
ref/out parameters and modify the array of objects if there is a ref/out
parameter.

Your call should be something like this.

// Set up the parameter modifiers.
ParameterModifier[] mods = new ParameterModifier[2]{new ParameterModifier(),
new ParameterModifier()};

// Set the second parameter to be allowed to be modified.
mods[1][0] = true;

// Make the call.
Myclass.InvokeMember("MyMethod", BindingFlags.InvokeMethod, null, obj,
parameters, mods, null, null);

Hope this helps.
 
Gus said:
Hi, I?m triying to invoke a member using reflection, but this member requiere
two parameters, the first patrameter is a string and the second is a
reference to a dataset (output parameter). The parameters has to be passed
in an array of objects.

I?ve tried something like this:

object[] parameters = new object[2];

parameters [0] ="text";

DataSet DS=new DadaSet();
parameters [1]= DS;

Myclass.InvokeMember("MyMethod",BindingFlags.InvokeMethod,null,
obj,parameters );

But it does?nt work because the second item of the array (parameters[1])
should be a refference to a dataset.... How can I solve this problem?
Thanks

It shouldn't be a problem - could you post a short but complete program
which demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Normally out parameters just end up in the parameters array without any
extra work.
 
Nicholas Paldino said:
You will have to use the overload of InvokeMember that takes an array of
ParameterModifier instances. This will tell reflection to pay attention to
ref/out parameters and modify the array of objects if there is a ref/out
parameter.

I don't understand why - it normally works fine without doing that,
even using out and ref parameters. Do you have an example where it's
needed? I *seem* to remember hearing it being something to do with COM
before, but that's probably a dodgy memory.
 
Jon,

Yes, it has something to do with calling members on COM components. The
OP used the ParameterModifiers array, which led me to believe that he was
using a COM component.

But normally, it is not needed.
 
Sorry to ask, where do you see the ParameterModifier in the OP posting?
I don't see any defined nor used in the InvokeMember call he posted.

Willy.

Nicholas Paldino said:
Jon,

Yes, it has something to do with calling members on COM components.
The OP used the ParameterModifiers array, which led me to believe that he
was using a COM component.

But normally, it is not needed.
 
Jon Skeet said:
I don't understand why - it normally works fine without doing that,
even using out and ref parameters. Do you have an example where it's
needed? I *seem* to remember hearing it being something to do with COM
before, but that's probably a dodgy memory.

That's right, this is only needed when using reflection when late binding to
COM object members, the CLR interop layer has no idea whether you need to
pass the argument(s) by ref or pass by value. The default being by value,
you need to indicate that you want to pass byref by means of a parameter
modifier.

Willy.
 
That's a mistake on my part. I saw the "parameters" variable and was
confused.
Willy Denoyette said:
Sorry to ask, where do you see the ParameterModifier in the OP posting?
I don't see any defined nor used in the InvokeMember call he posted.

Willy.

Nicholas Paldino said:
Jon,

Yes, it has something to do with calling members on COM components.
The OP used the ParameterModifiers array, which led me to believe that he
was using a COM component.

But normally, it is not needed.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jon Skeet said:
You will have to use the overload of InvokeMember that takes an
array of
ParameterModifier instances. This will tell reflection to pay
attention to
ref/out parameters and modify the array of objects if there is a
ref/out
parameter.

I don't understand why - it normally works fine without doing that,
even using out and ref parameters. Do you have an example where it's
needed? I *seem* to remember hearing it being something to do with COM
before, but that's probably a dodgy memory.
 
HI, thanks everybody.

I´ve tried the code that Nicholas Paldino gave me . but it says "Object
reference not set to an instance of an object" in the line : mods[1][0] =
true;

please.... keep helping me, I´m just a begginer in this matters.
 
Gus said:
I´ve tried the code that Nicholas Paldino gave me . but it says "Object
reference not set to an instance of an object" in the line : mods[1][0] =
true;

Yep, Nicholas had that wrong in his sample code. You have to pass in the
needed dimension to the ParameterModifier constructor. In his sample,
try changing "new ParameterModifier()" to "new ParameterModifier(1)",
that should do it.


Oliver Sturm
 
OK...it worked great.
Thanks

Oliver Sturm said:
Gus said:
I´ve tried the code that Nicholas Paldino gave me . but it says "Object
reference not set to an instance of an object" in the line : mods[1][0] =
true;

Yep, Nicholas had that wrong in his sample code. You have to pass in the
needed dimension to the ParameterModifier constructor. In his sample,
try changing "new ParameterModifier()" to "new ParameterModifier(1)",
that should do it.


Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
 

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