日期:2011-07-09  浏览次数:20803 次

我们先看看一般的反射的动态方法查找

下面为ms自带的例子ms-help://MS.VSCC/MS.MSDNVS.2052/cpref/html/frlrfsystemreflectionmethodbaseclassinvoketopic.htm

public class A

{

   public virtual int method () {return 0;}

}

public class B

{

   public virtual int method () {return 1;}

}

class Mymethodinfo

{

   public static int Main()

   {

      Console.WriteLine ("\nReflection.MethodInfo");

      A MyA = new A();

      B MyB = new B();

      //Get the Type and MethodInfo

      Type MyTypea = Type.GetType("A");

      MethodInfo Mymethodinfoa = MyTypea.GetMethod("method");

      Type MyTypeb = Type.GetType("B");

      MethodInfo Mymethodinfob = MyTypeb.GetMethod("method");

      //Get and display the Invoke method

      Console.Write("\nFirst method - " + MyTypea.FullName +

         " returns " + Mymethodinfoa.Invoke(MyA, null));

      Console.Write("\nSecond method - " + MyTypeb.FullName +

         " returns " + Mymethodinfob.Invoke(MyB, null));

      return 0;

   }

}

下面是用接口查询方法,实例创建对象,再执行实例对象

using System;

 public interface  IPoint

{

      // return  now     class of  shix    interface

        string  ReturnNowClass();

}   

上面文件为 ClassSuc.cs 编辑为ClassSuc.dll  。

using System;

namespace ClassLib1

{

     public class Class1:  IPoint

     {

         public Class1()

         {

         }

         public string ReturnNowClass()

         {

            return  "weclone  Execute ClassLib1   Class1";

         }

     }

}

将ClassSuc.dll的也添加到上面文件,Class1实现了Ipoint接口

编辑文件为ClassLib1.dll

using System;

namespace ClassLib2

{

     public class Class2:IPoint

     {

         public Class2()

         {

             

         }

         public string ReturnNowClass()

         {

              return  "ClassLib2"+"Class2"+"weclone";

         }

     }

}

将ClassSuc.dll的也添加到上面文件,Class2实现了Ipoint接口

编辑文件为ClassLib2.dll

也许你已经看和做的不厌烦了,你可能要要问,你到底想讲什么????

注意,将上面的三个dll  copy 在 同一路径下 这里为“C:/test”

using System;

using System.Reflection;

class LoadExe

     {

         [STAThread]

         static void Main(string[] args)