动态加载程序集(三)

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

下面为 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)

{

// 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

感谢 bitfan(数字世界一凡人) ( ) 使得自己有这样的收获

Published At
Categories with Web编程
Tagged with
comments powered by Disqus