Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / August 2004

Tip: Looking for answers? Try searching our database.

Reflection pass by ref abnormal behavior

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sri Vobilisetti - 27 Aug 2004 14:29 GMT
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
Mattias Sj?gren - 27 Aug 2004 16:37 GMT
Sri,

>        mi.Invoke(m,ps);

Insert

h["ABC"] = ps[0];

here
>        Console.WriteLine(h["ABC"]); //**output is "Initial value"
>                                //**But I expect it to be "Changed value"

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.