G
Guest
hi,
I am trying to call a method from a .net class dynamically and I am passing
a string reference to the method. Below is a sample of what I am doing.
Somehow the value of the parameter passed is not being updated. The wierd
thing is that if I change the variable type to Integer, it works.
====================================================
using System;
using System.Reflection;
using System.Collections;
namespace ReflectionTest
{
public class M
{
public void Method(ref string x)
{
x ="Changed value";
}
[STAThread]
public static void Main()
{
Hashtable h = new Hashtable();
h.Add("ABC", "Initial value"); //Set initial value
Type t = typeof(M);
M m = (M)Activator.CreateInstance(t);//Create instance of M
Type [] p = new Type[1];
p[0] = Type.GetType("System.String&");//typeof(ref string)
MethodInfo mi = t.GetMethod("Method", p);//Get the method
Object [] ps = {h["ABC"]}; //Create param array
mi.Invoke(m,ps);
Console.WriteLine(h["ABC"]); //**output is "Initial value"
//**But I expect it to be "Changed value"
}
}
}
====================================================
But if I change the variable type to Int, the value in the hashtable gets
updated. See sample below.
====================================================
using System;
using System.Reflection;
using System.Collections;
namespace ReflectionTest
{
public class M
{
public void Method(ref int x)
{
x =500;
}
[STAThread]
public static void Main()
{
Hashtable h = new Hashtable();
h.Add("ABC", 5); //Set initial value as 5
Type t = typeof(M);
M m = (M)Activator.CreateInstance(t);// Create instance of M
Type [] p = new Type[1];
p[0] = Type.GetType("System.Int32&");// typeof(ref int)
MethodInfo mi = t.GetMethod("Method", p);// Get the method
Object [] ps = {h["ABC"]}; // Create param array
mi.Invoke(m,ps);
Console.WriteLine(h["ABC"]); //*****output is 500
}
}
}
====================================================
Is this a kind of bug?
Thank you for your time.
Regards,
Sri Vobilisetti
I am trying to call a method from a .net class dynamically and I am passing
a string reference to the method. Below is a sample of what I am doing.
Somehow the value of the parameter passed is not being updated. The wierd
thing is that if I change the variable type to Integer, it works.
====================================================
using System;
using System.Reflection;
using System.Collections;
namespace ReflectionTest
{
public class M
{
public void Method(ref string x)
{
x ="Changed value";
}
[STAThread]
public static void Main()
{
Hashtable h = new Hashtable();
h.Add("ABC", "Initial value"); //Set initial value
Type t = typeof(M);
M m = (M)Activator.CreateInstance(t);//Create instance of M
Type [] p = new Type[1];
p[0] = Type.GetType("System.String&");//typeof(ref string)
MethodInfo mi = t.GetMethod("Method", p);//Get the method
Object [] ps = {h["ABC"]}; //Create param array
mi.Invoke(m,ps);
Console.WriteLine(h["ABC"]); //**output is "Initial value"
//**But I expect it to be "Changed value"
}
}
}
====================================================
But if I change the variable type to Int, the value in the hashtable gets
updated. See sample below.
====================================================
using System;
using System.Reflection;
using System.Collections;
namespace ReflectionTest
{
public class M
{
public void Method(ref int x)
{
x =500;
}
[STAThread]
public static void Main()
{
Hashtable h = new Hashtable();
h.Add("ABC", 5); //Set initial value as 5
Type t = typeof(M);
M m = (M)Activator.CreateInstance(t);// Create instance of M
Type [] p = new Type[1];
p[0] = Type.GetType("System.Int32&");// typeof(ref int)
MethodInfo mi = t.GetMethod("Method", p);// Get the method
Object [] ps = {h["ABC"]}; // Create param array
mi.Invoke(m,ps);
Console.WriteLine(h["ABC"]); //*****output is 500
}
}
}
====================================================
Is this a kind of bug?
Thank you for your time.
Regards,
Sri Vobilisetti