G
Guest
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
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