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.Reflecti on;
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.WriteLi ne(sMyNum);
ParameterModifi er pm = new ParameterModifi er(1);
pm[0] = true;
ParameterModifi er [] mods = { pm };
object [] MyArgs = new object[1];
MyArgs[0] = sMyNum;
Type tMyRef = MyRef.GetType() ;
tMyRef.InvokeMe mber("CalcByRef ",BindingFlags. InvokeMethod,nu ll,MyRef,
MyArgs,mods,nul l,null);
Console.WriteLi ne(sMyNum);
} // try
catch (Exception ex)
{
Console.WriteLi ne(ex.Message);
} // catch (Exception ex)
} // static void Main ()
} // class TestByRef
} // namespace TestByRef 3 10173
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.Reflecti on; 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.WriteLi ne(sMyNum); ParameterModifi er pm = new ParameterModifi er(1); pm[0] = true; ParameterModifi er [] mods = { pm }; object [] MyArgs = new object[1]; MyArgs[0] = sMyNum; Type tMyRef = MyRef.GetType() ; tMyRef.InvokeMe mber("CalcByRef ",BindingFlags. InvokeMethod,nu ll,MyRef, MyArgs,mods,nul l,null); Console.WriteLi ne(sMyNum); } // try catch (Exception ex) { Console.WriteLi ne(ex.Message); } // catch (Exception ex) } // static void Main () } // class TestByRef } // namespace TestByRef
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.Reflecti on; 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.WriteLi ne(sMyNum); ParameterModifi er pm = new ParameterModifi er(1); pm[0] = true; ParameterModifi er [] mods = { pm }; object [] MyArgs = new object[1]; MyArgs[0] = sMyNum; Type tMyRef = MyRef.GetType() ; tMyRef.InvokeMe mber("CalcByRef ",BindingFlags. InvokeMethod,nu ll,MyRef, MyArgs,mods,nul l,null); Console.WriteLi ne(sMyNum); } // try catch (Exception ex) { Console.WriteLi ne(ex.Message); } // catch (Exception ex) } // static void Main () } // class TestByRef } // namespace TestByRef
Pat Ireland <ir*******@eart hlink.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.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 http://support.microsoft.com/default.aspx?scid=kb;en-us;302901
I want to change the button_click event code so that, when the button is
pressed, a value is written at the cell A1 of the active sheet.
I tryied the following code:
|
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 following the Microsoft Q302901 article.
- This COM Add-in adds a button at the "Standard" Excel toolbar.
- When I press the button I want a value to be inserted on the current cell
of the active sheet.
- I want the add-in to user late-binding when...
|
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 slightly different arguments for
which I dynamically create the object arguments and it all works nicely.
Problem is, in some instances, I have methods which require certain
arguments to be referenced (ref string x,...etc). I'm not sure how I can...
|
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 that I use to communicate
to some unmanaged code in a DLL written in C. It is not complicated, but will
change and I would like to be able to sequentially access it without
explicitly referring to each and every element. Here is the structure:
|
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.
| |
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, which will
handle the audio somehow.
The JavaScript callback is a simple function, which I pass to the C#
object during its initialization; I then invoke the JavaScript
callback using
theCB.GetType().InvokeMember("", BindingFlags.InvokeMethod,...
|
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, leads me to this MS
Support Page:
http://support.microsoft.com/?scid=kb%3Ben-us%3B329080&x=6&y=12
Problem is, this page refers to WIN XP, whereas both my development and
production machines are W2k3 Servers (SP2, latest patch level). On the
|
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 five or six ip addresses that I
attempt to add to IIS causes an System.Reflection.TargetInvocationException
to be thrown. Here is the code that adds the addresses:
static void SetIPSecurityProperty(string metabasePath, string member,
string item)...
|
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 'onclick' event from
'cmdBusStopScheduleSubmit' element - see below.
ONCLICK
=========
<input type="submit" name="cmdBusStopScheduleSubmit" value="Get Bus Stop
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |