动态加载程序集(三) (转)

本文通过实例演示了如何使用反射机制动态加载程序集,并展示了如何根据条件选择加载不同的DLL文件。介绍了通过接口查询方法及创建并执行实例对象的过程。
动态加载程序集(三) (转)[@more@]

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

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

public class AXML:namespace prefix = o ns = "urn:schemas-microsoft-com:Office:office" />

{

  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)

  {

  // Use the file name to load the assembly into the current application domain.

  Assembly b;

  b = Assembly.LoadFrom(@"C:/test/ClassSuc.dll");

  Type[] mytypes = b.GetTypes();

  // show b中的接口IPoint

  foreach (Type t in mytypes)

  {

  Console.WriteLine (t.FullName);

  }

  MethodInfo Method = mytypes[0].GetMethod("ReturnNowClass");

  //Get the method to call.

  Assembly a ;

  string  k=Console.ReadLine();

  //输入大于10时,调用ClassLib1.dll的方法 否则调用ClassLib2的方法

    if(Convert.ToInt32(k)>10)

  a = Assembly.LoadFrom(@"C:/test/ClassLib1.dll");

  else

  a = Assembly.LoadFrom(@"C:/test/ClassLib2.dll");

  Type[] types = a.GetTypes();

  // show b中的ClassLib1.Class1或 ClassLib2.Class2

  foreach (Type t in  types)

  {

  Console.WriteLine (t.FullName);

  }

  // Create an instance of the HelloWorld class.

  object obj = Activator.CreateInstance(types[0]);

  // Invoke the  method.

  Console.WriteLine(Method.Invoke(obj, null)); 

  Console.ReadLine();

 

  }

  }

执行效果为:

Ipoint

这时要求输入 输入

13

继续执行显示

ClassLib1.Class1

weclone  Execute ClassLib1  Class1

要求输入时 输入

5

继续执行显示

ClassLib2.Class2

weclone  Execute ClassLib2  Class2

实现了什么,通过接口动态加载程序集。

意义:反射机制实现动态插拔,只需更改配置文件和XCOPY相应的组件

可以无须编译就直接定制出一个特定系统

  缺点: 性能冲击 速度慢

 

有的人还要问,既然能够动态加载程序集

那如何显示卸载程序集  CLR不支持卸载程序集  但可以卸载AppDomain包含的所有的程序集。AppDomain.Unload 方法 卸载指定的应用程序域。

本还想写一文章讲述动态程序集 但自己理解不够,又觉得意义不大所以把那些东西,自己也不想学那些东西(太浮躁的表现),所以提到这里来。

动态程序集是编译器或工具在运行时发出元数据和 MSIL,并可在磁盘上生成可移植可执行 (PE) 文件 (不同于上面的那个动态加载程序集)

在运行时定义程序集,然后运行这些程序集并/或将它们保存到磁盘。

在运行时定义新程序集中的模块,然后运行这些模块并/或将它们保存到磁盘。

在运行时定义类型,创建这些类型的实例,并调用这些类型的方法。

程序集---》 模块---》类型—》实例的方法

具体见

ms-help://MS.NETframeworkSDK.CHS/cpguidenf/html/cpconemittingdynamicassemblies.htm

ms-help://MS.VSCC/MS.MSDNVS.2052/cpref/html/frlrfsystemappdomainclassdefinedynamicassemblytopic.htm

感谢ASP?sendto=bitfan" target=_blank>bitfan(数字世界一凡人) ( )  使得自己有这样的收获


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-997906/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/10752043/viewspace-997906/

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值