反射学习

namespace ReflectionTest
{
   public class test
    {
       public void writeString(string s)
       {
           Console.WriteLine("writeString:" + s);
       }
       public static void staticWrite(string s)
       {
           Console.WriteLine("staticString:" + s);
       }
       public string addString(string s)
       {
           return s + s;
       }
    }
}

 

调用    记得调用  using System.Reflection;

     static void Main(string[] args)
        {
            Assembly dll = Assembly.LoadFile(@"C:\Users\Fox\Desktop\反射学习\ReflectionTest\bin\Debug\ReflectionTest.dll");
            Type type = dll.GetType("ReflectionTest.test");
            Console.WriteLine(type.FullName);
            MethodInfo mi = type.GetMethod("writeString");
            object obj = dll.CreateInstance("ReflectionTest.test");
            Object[] oo = new Object[] { "wq" };
            //调用非静态方法 第一个参数得为其创建一个实例
            mi.Invoke(obj, oo);
            mi = type.GetMethod("staticWrite");
            //调用静态方法 第一个参数可以为NULL
            mi.Invoke(null, oo);
            Console.ReadLine();
        }

  

 

 

posted on 2013-09-13 11:09  奋斗的笨小孩  阅读(228)  评论(0编辑  收藏  举报