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 2005

Tip: Looking for answers? Try searching our database.

Passing Array using Reflection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Vignesh - 11 Aug 2005 12:55 GMT
Hi ,
 Can anybody help me why this code is failing?
 I am passing an array using reflection.

using System;
using System.Reflection;
using System.Collections;

namespace ReflectionTest
{
    public class M
    {
        public void Method(string[] x)
        {
            Console.WriteLine(x[0]);
            //x ="Changed value";
           
        }
        [STAThread]
        public static void Main()
        {
            Hashtable h = new Hashtable();
            h.Add("ABC", "Initial value"); //Set initial value
            h.Add("BCD", "Second value"); //Set second value
            Type t = typeof(M);
            M m = (M)Activator.CreateInstance(t);//Create instance of M
            Type [] p = new Type[2];
           
            p[0] = Type.GetType("System.String");//typeof(ref string)
            p[1] = Type.GetType("System.String");//typeof(ref string)
            MethodInfo mi = t.GetMethod("Method", p);//Get the method
            Object [] ps = {h["ABC"],h["BCD"]}; //Create param array
            mi.Invoke(m,ps);
            Console.WriteLine(h["ABC"]); //**output is "Initial value"
            Console.WriteLine(ps[0].ToString());
           
        }

    }
}

Thanks in advance.

- Vignesh
Erick Sgarbi - 11 Aug 2005 13:26 GMT
Considering that your method is taking an array of Strings...
> public void Method(string[] x)

The method signature is not 2 strings but rather an array of strings:
    Type [] p = new Type[];
    p[0] = Type.GetType("System.String[]");

Next you are passing an array of objects from the Hashtable indexer
instead of an array of string as well as are not passing the appropriate
argument... you must pass an array of objects relating to the signature
which is an array of strings (string[]):
> Object [] ps = {h["ABC"],h["BCD"]};

It should be:
    System.Object [] ps = new System.Object[]
                {
                    new System.String[]
                    {
                        h["ABC"].ToString(),
                        h["BCD"].ToString()
                    }
                };

HTH
Erick Sgarbi
www.blog.csharpbox.com

> Hi ,
>   Can anybody help me why this code is failing?
[quoted text clipped - 40 lines]
>
> - Vignesh

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.