473,385 Members | 1,958 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,385 software developers and data experts.

Problem with explicit boxing for InvokeMember

I continue to investigate issues with making explicit InvokeMember calls
(necessary when dynamically loading classes). My quest lets me use a simple
class to invoke another class's member method by normal means and by using
the InvokeMember. By examining the underlying il code with ildasm, when
attempting to pass by reference, the il code generates a box command so that
a pointer to the actual data is passed. This is clearly seen with the Normal
invocation call at the il level. However, when I try to creat a InvokeMember
call, I have been unable to load a Int16& in the object list that represents
the calling parameters. The type stored in the MyCallingArgs[0] is a
System.Reflection.Pointer rather than the desired Int16&. I am attaching the
code so that you may easily test the issue.

Any clues as to how to make the second call correctly?

using System;
using System.Reflection;
namespace TestByReference
{
class MyClass
{
public void MyMethod (ref short MyParm)
{
MyParm++;
} // public void MyMethod ()
} // class MyClass
class TestByReference
{
[STAThread]
static void Main(string[] args)
{
try
{
// Normal invocation
MyClass MyObj = new MyClass();
short MyArg = 5;
Console.WriteLine("MyArg {0}",MyArg);
MyObj.MyMethod(ref MyArg);
Console.WriteLine("MyArg {0}",MyArg);
// InvokeMember
Type MyType = MyObj.GetType();
Console.WriteLine("MT {0}",MyType.FullName);
MethodInfo [] MyInfos = MyType.GetMethods();
foreach(MethodInfo mInfo in MyInfos)
Console.WriteLine("Method {0}",mInfo.Name);
Console.WriteLine("# MIs {0}",MyInfos.Length);
Console.WriteLine("MI[3] {0}",MyInfos[3].Name);
ParameterInfo [] MyParms;
MyParms = MyInfos[3].GetParameters();
Console.WriteLine("# MPs {0}",MyParms.Length);
Console.WriteLine("Type of Parmeter {0}",
MyParms[0].ParameterType);
object [] MyCallingArgs = new object [1];
unsafe
{
MyCallingArgs[0] =
Pointer.Box(&MyArg,typeof(System.Int16 *));
Console.WriteLine("MyCallingArs[0] type
{0}",MyCallingArgs[0].GetType());
MyType.InvokeMember("MyMethod",
BindingFlags.InvokeMethod |
BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance,
null,
MyObj,
MyCallingArgs);
} // unsafe
Console.WriteLine("MyArg {0}",MyArg);
} // try
catch (Exception ex)
{
Console.WriteLine(ex.Message);
} // catch (Exception ex)
} // static void Main ()
} // class TestByReference
} // namespace TestByReference

Nov 17 '05 #1
0 1290

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

Similar topics

5
by: Francois Malgreve | last post by:
in the following code: object obj = this.ViewState; string s = obj.GetType().ToString(); currentCreditLimit = (double) this.ViewState; ViewState is a StateBag object (dictionary type object)...
0
by: Alex Zhitlenok | last post by:
Hi, On Google, one can find a lot of statements that Invoke and InvokeMemeber work alike. However, my own experience contradicts this statement. 1. Surprisingly, Invoke and InvokeMember methods...
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
0
by: eirik hoem | last post by:
I have a problem using InvokeMember. I have a client application that loads a set of assemblies runtime. It creates instances of them and calls a method which I specify in a XML file. What...
0
by: eirik hoem | last post by:
case-block was followed by a ; that's what you get for 13hrs straight ------------------------ I have a problem using InvokeMember. I have a client application that loads a set of assemblies...
3
by: Pat Ireland | last post by:
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...
4
by: Mathieu Cartoixa | last post by:
Hi, I have been annoyed in one of my recent projects with a problem related to the explicit implementation of an interface on a value type. I will take an example to show the problem. Say we...
14
by: Noone | last post by:
Hello all, Ok, I want to create a program that will load plugins (dll's) from a plugin folder. I can create the forms and put them into a dll but I cannot actually add them dynamically at run...
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.