Reflection pass by ref abnormal behavior

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
 
J

Jon Skeet [C# MVP]

Sri Vobilisetti said:
Thank you very much Mattias. This is not exactly I am thinking about, but
will use as a work around. By the way is there any way to get the value to
the hashtable directly. If you look at the other example in my question which
involves passing a int type. It gets the value into the hashtable without
having to retrieve it from the object[]. Do you know what the difference is
between the string and int implementation?

In the int implementation, I believe you're actually getting the boxed
value from the Hashtable, and changing the value within the box. It's
very interesting though - I hadn't seen that behaviour before. If you
change the code to use:

Object [] ps = {(int)h["ABC"]}; // Create param array

then there's no change in the hashtable because the value is unboxed
then reboxed.

I'm not even sure whether the behaviour you're showing is correct or
not...
 

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