G
Guest
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.InvokeMethod,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
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.InvokeMethod,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