.NETGURU
Reflection pass by ref abnormal behavior
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.sdk.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Sri Vobilisetti (VIP)
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? Any ideas on getting this to work with strings.

Thank you for your time.

Regards,
Sri Vobilisetti

Reply to this message...
 
    
Andriy Gnennyy
Check ps[] after Invoke. The ps[0] = = "Changed value".

--
Andriy Gnennyy
pulsar2003@{no-spam}email.ru (Please remove {no-spam} for reply)
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx

"Sri Vobilisetti" <Click here to reveal e-mail address> wrote in
message news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Sri Vobilisetti (VIP)
Thank you for your help.

"Andriy Gnennyy" wrote:

[Original message clipped]

Reply to this message...
 
 
System.Activator
System.Collections.Hashtable
System.Console
System.Int32
System.Reflection.MethodInfo
System.String
System.Type




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification