473,320 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How do I correctly pass by ref using InvokeMember?

The following code shows what I am trying to do. The normal invocation of
the CalcByRef produces the correct results, however, using the InvokeMember
fails to correctly pass the single argument by reference. How can I
correctly pass the short by ref through a InvokeMember.

using System;
using System.Reflection;
namespace TestByRef
{
class DoByRef
{
public void CalcByRef (ref short sNum)
{
sNum++;
} // public void CalcByRef ()
} // class DoByRef
class TestByRef
{
[STAThread]
static void Main (string[] args)
{
try
{
short sMyNum = 5;
DoByRef MyRef = new DoByRef ();
MyRef.CalcByRef(ref sMyNum);
Console.WriteLine(sMyNum);
ParameterModifier pm = new ParameterModifier(1);
pm[0] = true;
ParameterModifier [] mods = { pm };
object [] MyArgs = new object[1];
MyArgs[0] = sMyNum;
Type tMyRef = MyRef.GetType();
tMyRef.InvokeMember("CalcByRef",BindingFlags.Invok eMethod,null,MyRef,
MyArgs,mods,null,null);
Console.WriteLine(sMyNum);
} // try
catch (Exception ex)
{
Console.WriteLine(ex.Message);
} // catch (Exception ex)
} // static void Main ()
} // class TestByRef
} // namespace TestByRef

Nov 17 '05 #1
3 10147
Rather than checking the value of sMyNum at the end, try checking the value
of MyArgs[0].

You are copying the value of sMyNum into the args array and the passing that
(not sMyNum) to CalcByRef which is making the adjustment to the value in the
array.

Brendan
"Pat Ireland" wrote:
The following code shows what I am trying to do. The normal invocation of
the CalcByRef produces the correct results, however, using the InvokeMember
fails to correctly pass the single argument by reference. How can I
correctly pass the short by ref through a InvokeMember.

using System;
using System.Reflection;
namespace TestByRef
{
class DoByRef
{
public void CalcByRef (ref short sNum)
{
sNum++;
} // public void CalcByRef ()
} // class DoByRef
class TestByRef
{
[STAThread]
static void Main (string[] args)
{
try
{
short sMyNum = 5;
DoByRef MyRef = new DoByRef ();
MyRef.CalcByRef(ref sMyNum);
Console.WriteLine(sMyNum);
ParameterModifier pm = new ParameterModifier(1);
pm[0] = true;
ParameterModifier [] mods = { pm };
object [] MyArgs = new object[1];
MyArgs[0] = sMyNum;
Type tMyRef = MyRef.GetType();
tMyRef.InvokeMember("CalcByRef",BindingFlags.Invok eMethod,null,MyRef,
MyArgs,mods,null,null);
Console.WriteLine(sMyNum);
} // try
catch (Exception ex)
{
Console.WriteLine(ex.Message);
} // catch (Exception ex)
} // static void Main ()
} // class TestByRef
} // namespace TestByRef

Nov 17 '05 #2
Hi Brendan,

Yes, I agree that the correct value of sMyNum can be found in the MyArgs[0].
But then the two calls are not equivalent. If you examine the underlying il
code you will find that the C# compiler uses a load address of the argument.
However, with the InvokeMember I can not find a way to have the original
variable location modified as in MyRef.CalcByRef(ref sMyNum). If I am
required to copy the MyArgs[0] back into sMyNum after a InvokeMember then it
is not a pass by reference. I spent a lot of time trying to get the address
of sMyNum (a short *) into an object, but C# complains. Unsafe and fixed
statements only get me part of the way. So do you have any other suggestions
because at this point I am squeezed dry of ideas.

Pat

"Brendan Grant" wrote:
Rather than checking the value of sMyNum at the end, try checking the value
of MyArgs[0].

You are copying the value of sMyNum into the args array and the passing that
(not sMyNum) to CalcByRef which is making the adjustment to the value in the
array.

Brendan
"Pat Ireland" wrote:
The following code shows what I am trying to do. The normal invocation of
the CalcByRef produces the correct results, however, using the InvokeMember
fails to correctly pass the single argument by reference. How can I
correctly pass the short by ref through a InvokeMember.

using System;
using System.Reflection;
namespace TestByRef
{
class DoByRef
{
public void CalcByRef (ref short sNum)
{
sNum++;
} // public void CalcByRef ()
} // class DoByRef
class TestByRef
{
[STAThread]
static void Main (string[] args)
{
try
{
short sMyNum = 5;
DoByRef MyRef = new DoByRef ();
MyRef.CalcByRef(ref sMyNum);
Console.WriteLine(sMyNum);
ParameterModifier pm = new ParameterModifier(1);
pm[0] = true;
ParameterModifier [] mods = { pm };
object [] MyArgs = new object[1];
MyArgs[0] = sMyNum;
Type tMyRef = MyRef.GetType();
tMyRef.InvokeMember("CalcByRef",BindingFlags.Invok eMethod,null,MyRef,
MyArgs,mods,null,null);
Console.WriteLine(sMyNum);
} // try
catch (Exception ex)
{
Console.WriteLine(ex.Message);
} // catch (Exception ex)
} // static void Main ()
} // class TestByRef
} // namespace TestByRef

Nov 17 '05 #3
Pat Ireland <ir*******@earthlink.net> wrote:
Yes, I agree that the correct value of sMyNum can be found in the MyArgs[0].
But then the two calls are not equivalent. If you examine the underlying il
code you will find that the C# compiler uses a load address of the argument.
However, with the InvokeMember I can not find a way to have the original
variable location modified as in MyRef.CalcByRef(ref sMyNum). If I am
required to copy the MyArgs[0] back into sMyNum after a InvokeMember then it
is not a pass by reference.
It's not a pass by reference of sMyNum, but it never could be, given
that when you copy the value into the parameter array, that's all
you're copying - the value.
I spent a lot of time trying to get the address
of sMyNum (a short *) into an object, but C# complains. Unsafe and fixed
statements only get me part of the way. So do you have any other suggestions
because at this point I am squeezed dry of ideas.


Yes - just copy the value back from the parameter array into sMyNum.
You're not going to find any other way of getting pass-by-reference
semantics via reflection.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Emmanuel | last post by:
Hi, I 've managed to run successfully the example provided by the Microsoft Q302901 - "How To Build an Office COM Add-in by Using Visual C# .NET" article...
0
by: Emmanuel | last post by:
Hi everybody, I have already opened a thread with the same question but since I got no answers I will try to rephrase my question and be more specific. - I have already build a COM Add-In...
1
by: Rein Petersen | last post by:
Hi All, I'm invoking Type.InvokeMember() on a COM class (via COMInterop) through a generalized interface. However, each specialized instance (of the COMInterop generic interface) requires...
9
by: Bill Grigg | last post by:
All, Can anyone supply an example or reference to an example of using reflection to determine the data types and array lengths contained in a nested stucture in C#? Actually, it is a structure...
0
by: sugee | last post by:
i invoke a COm method which has a out paramter. i invoke that method using invoke member. nw i should pass the value by reference using invokemember. so need some help.
1
by: thomas_okken | last post by:
I'm working on a little C# DLL that will let me capture audio from within Internet Explorer. The idea is that the C# object, once it is recording, will periodically invoke a JavaScript callback,...
11
by: =?ISO-8859-15?Q?Tobias_Schr=F6er?= | last post by:
Hi, I am running into problems when performing an RPC call on a COM object via Type.InvokeMember (.Net 2.0 Framework). The exception thrown is "HRESULT: 0x800706BE", which, after some googling,...
1
by: tommyk | last post by:
I am writing a small program that reads a text file filled with a large number of IP addresses and attempts to add them to IIS so that they will be blocked. For some reason, about one in every...
4
by: James | last post by:
Hello everyone, While loading a page (http://www.edmonton.ca/portal/server.pt?space=CommunityPage&control=SetCommunity&CommunityID=239) into a webbrowser control I use invokemember on the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.