假设有一个类Public class KalsTest
{
   Public string Test(string inputStr)
{
   String str = “Kals” + inputStr.ToString();
}
}
请写一个方法,通过变量得到类名,然后动态的去调用Test方法。(提示:使用反射机制)。

解决方案 »

  1.   

    using System;
    using System.Reflection;namespace CSharpConsole05
    {
        public class KalsTest
        {
            public string Test(string inputStr)
            {
                String str = "Kals" + inputStr.ToString();
                return str;
            }
        }    class Program
        {
            static void Main(string[] args)
            {
                Assembly asm = Assembly.GetExecutingAssembly();
                Type t = asm.GetType("CSharpConsole05.KalsTest");
                object obj = Activator.CreateInstance(t);
                string result = t.GetMethod("Test").Invoke(obj, new object[] { " called" }) as string;
                Console.WriteLine(result);
                Console.ReadKey();
            }
        }
    }