Convert short * to object for use with InvokeMember

  • Thread starter Patrick Ireland
  • Start date

P

Patrick Ireland

I am dynamically loading a class. One of the methods of the class takes
a short * (by reference) argument. However, the InvokeMember call used
to invoke the method of the class passings arguments in and object [].
Since this is a pointer reference being passed it must be invoked in a
unsafe {} even thought the dynamically loaded class is a .Net entity.
The following is the test code that I am using to try and load a short *
into an object:


using System;
using System.Reflection;
namespace TestBoxing
{
class TestBoxing
{
[STAThread]
static void Main(string[] args)
{
Object MyObj;
Int16 MyVar = 1;
unsafe
{
System.Int16 * pVar = &MyVar;
MyObj = Pointer.Box(pVar,typeof(System.Int16 *));
Console.WriteLine("Type {0}",MyObj.GetType());
} // unsafe
} // static void Main ()
} // class TestBoxing
} // namespace TestBoxing


A more complete tester for reflection and trying to use InvokeMember is
as follows:


using System;
using System.Reflection;
namespace TestByReference
{
class MyClass
{
public void MyMethod (ref short MyParm)
{
MyParm = 6;
} // 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();
Console.WriteLine("# MIs {0}",MyInfos.Length);
foreach(MethodInfo mInfo in MyInfos)
Console.WriteLine("Method {0}",mInfo.Name);
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

As a finally comment on my question - if you examine the underlying il
code the C# invocation does a boxing of the short *, but attempting to
use the InvokeMember can not construct the required argument list.
 
Ad

Advertisements

W

Willy Denoyette [MVP]

No need to resort to unsafe constructs, use the ParameterModifier and pass
the value byref like this:

MyCallingArgs[0] = MyArg;
ParameterModifier p = new ParameterModifier(1);
p[0] = true; // pass arg0 by ref
ParameterModifier[] mods = { p };

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,
mods, null, null);

Willy.

Patrick Ireland said:
I am dynamically loading a class. One of the methods of the class takes
a short * (by reference) argument. However, the InvokeMember call used
to invoke the method of the class passings arguments in and object [].
Since this is a pointer reference being passed it must be invoked in a
unsafe {} even thought the dynamically loaded class is a .Net entity.
The following is the test code that I am using to try and load a short *
into an object:


using System;
using System.Reflection;
namespace TestBoxing
{
class TestBoxing
{
[STAThread]
static void Main(string[] args)
{
Object MyObj;
Int16 MyVar = 1;
unsafe
{
System.Int16 * pVar = &MyVar;
MyObj = Pointer.Box(pVar,typeof(System.Int16 *));
Console.WriteLine("Type {0}",MyObj.GetType());
} // unsafe
} // static void Main ()
} // class TestBoxing
} // namespace TestBoxing


A more complete tester for reflection and trying to use InvokeMember is
as follows:


using System;
using System.Reflection;
namespace TestByReference
{
class MyClass
{
public void MyMethod (ref short MyParm)
{
MyParm = 6;
} // 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();
Console.WriteLine("# MIs {0}",MyInfos.Length);
foreach(MethodInfo mInfo in MyInfos)
Console.WriteLine("Method {0}",mInfo.Name);
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

As a finally comment on my question - if you examine the underlying il
code the C# invocation does a boxing of the short *, but attempting to
use the InvokeMember can not construct the required argument list.
 
P

Patrick Ireland

Willy, Thank you for replying. However, I have tried that very solution
also. If you print out the value of MyArg after the InvokeMember call
using the ParameterModifier method, the value is the same as passed to
the function. This indicates that a byref must be place in the
MyCallingArgs[0] - or at least it indicates that to me. Unfortunately,
there is no connection made between the object[] argument list and the
ParameterModifier list until the actual invoke. Doing the assignmen of
the MyCallingArgs[0]=MyArg; just takes the value contained in MyArg (6
at the point), makes it an object and puts in the the 0th element of the
calling parameters. The assignment of a reference to MyArg needs to be
made when MyCallingArgs[0] is loaded. I have tried to use unsafe and
fixed to try and force a short * in the calling parameters but have not
been able to so far. By examining the underlying il code, I can tell
the the C# compiler boxes the reference when the invoked method is in a
class that has not been dynamically loaded (the dead give away is the
box instruction). I have not been able to generate equivalent code when
the class is dynamically loaded.

Do you have any more suggestions?

I have posted this very same question on the Microsoft C# user's group
but no one ever responded. I thank you for your efforts but I am at a
loss as to what to try next.

Pat, MCSD
 
Ad

Advertisements

P

Patrick Ireland

Willy,

Correction to my last response. I turned on the include source listing
in ildasm and quickly discovered the box command is being used with the
Console.WriteLine command. However, examining more carefully this time,
it is clear that the C# compiler is loading the address of the short
prior to invoking the method. It uses the ldloca.s which is a load the
address of a short command. Which brings me back the how do I do that
using the invokemember or invokemethod reflection call? The problem is
that C# is not allowing me to cast a short * to an object, so I can load
it into the args list which is a object []

Pat, MCSD, plus lots of other letters
 

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

Top