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.Invok eMethod,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 12 6929
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
--
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
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Gus" <Gu*@discussions.microsoft.com> wrote in message
news:AC**********************************@microsof t.com... 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.Invok eMethod,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
Great, that looks cleaner than my solution.
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
Gus <Gu*@discussions.microsoft.com> wrote: 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.Invok eMethod,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.
--
Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote: 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 Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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]
- mv*@spam.guard.caspershouse.com
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote: 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 Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:uI*************@TK2MSFTNGP15.phx.gbl... 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] - mv*@spam.guard.caspershouse.com
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:MP************************@msnews.microsoft.c om... Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote: 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 Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om... Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote: 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 Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
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 [MVP]" <wi*************@telenet.be> wrote in message
news:%2****************@tk2msftngp13.phx.gbl... 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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:uI*************@TK2MSFTNGP15.phx.gbl... 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] - mv*@spam.guard.caspershouse.com
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:MP************************@msnews.microsoft.c om... Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote: 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 Skeet - <sk***@pobox.com> http://www.pobox.com/~skeet If replying to the group, please do not mail me too
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 wrote: 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
OK...it worked great.
Thanks
"Oliver Sturm" wrote: Gus wrote:
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Derek Hart |
last post by:
I wish to execute code from a string. The string will have a function name,
which will return a string:
Dim a as string
a = "MyFunctionName(param1, param2)"
I have seen a ton of people...
|
by: Robert May |
last post by:
We have a VB6 COM component that I don't have control over. All of the
values are returned from method calls using byref parameters. They
frequently break binary compatibility, in fact, there are...
|
by: Mike Malter |
last post by:
I am just starting to work with reflection and I want to create a log that saves relevant information if a method call fails so I
can call that method again later using reflection.
I am...
|
by: jerry051 |
last post by:
I invoke a method by reflection, when this method's parameters is simple
type like int or string,the invoking is correct and secceed. But when the
parameters is ArrayList type, debuger tips me that...
|
by: Jason |
last post by:
Hello,
I"m trying to invoke a method using reflection on a DLL that is already
within a project. However, I"m having a few problems and didn't know if
anyone has any bright ideas. Please note,...
|
by: bowser |
last post by:
Hello,
I'm new to C# but so far I didn't find big problems, except for
Reflection. I coudn't find any good material in internet where to study
it. If anyone knows about some documents, blogs or...
|
by: ranadhirnag |
last post by:
I need to check a property in the javax.faces.component.html.HtmlInputText.
If i do it in the following manner,I get things fine:...
|
by: Klaudiusz Bryja |
last post by:
Hi,
This is for NetCF 2.0.
I need to create event handling code which using reflection. I have
some parameters in XML which describe how event should be handled. I
have code to create...
|
by: Cralis |
last post by:
Hi guys,
Someone once said, 'You can do that with reflection'. I can't recall
what it was I was trying to do at the time, but then he said, 'Any
developer knows what reflection is...'. I kept...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |