王朝网络
分享
 
 
 

动态加栽程序集(二)

王朝other·作者佚名  2006-01-08
宽屏版  字体: |||超大  

正式开始本文:在 .NET 框架中,将程序集加载至应用程序域的方法有几种。每种方法使用不同的类。

您可以使用下面的重载方法将程序集加载至应用程序域:

System.AppDomain 类包含几种重载的 Load 方法。尽管这些方法可用于将任何程序集成功地加载至当前的或新的应用程序域,但它们主要还是用于 COM 交互操作。您也可以使用 CreateInstance 方法加载程序集。

System.Reflection.Assembly 类包含两种静态重载方法:Load 和 LoadFrom。这两种方法因加载上下文而异。

简单例题:讲解了在一个.exe文件中调用另一个.exe文件的方法

using System;

namespace dy_loadAsse

{

class testclass

{

[STAThread]

static void Main(string[] args)

{

OutUse test=new OutUse();

test.Output();

Console.ReadLine();

}

}

class OutUse

{

public OutUse()

{

}

public void Output()

{

Console.WriteLine("test dy load assembly");

}

}

}

以上编译成功。为dy_loadAsse.exe,执行显示:

test dy load assembly

放在与下面生成的loadexe.exe于同一目录下。

文件二:

using System;

using System.Reflection;

namespace Use_dy_Load_Assembly

{

class LoadExe

{

[STAThread]

static void Main(string[] args)

{

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

Assembly a = Assembly.LoadFrom("dy_loadAsse.exe");

Type [] types2 = a.GetTypes();

foreach (Type t in types2)

{

Console.WriteLine (t.FullName);

}

//Get the type to use.

//Type myType = a.GetType("OutUse"); 这样写老是出错

Type myType = a.GetType("dy_loadAsse.OutUse");

Console.WriteLine (myType.FullName);

//Get the method to call.

MethodInfo mymethod = myType.GetMethod("Output");

// //Create an instance.

Object obj = Activator.CreateInstance(myType);

// //Execute the adnamemethod method.

mymethod.Invoke(obj,null);

//执行结果为test dy load assembly

//以下是调用dy_loadAsse.exe中的Main方法,出现错误

// Type myType = a.GetType("dy_loadAsse.testclass");

// Console.WriteLine (myType.FullName);

// //Get the method to call.

// MethodInfo mymethod = myType.GetMethod("Main");

// //// //Create an instance.

// Object obj = Activator.CreateInstance(myType);

// //// //Execute the adnamemethod method.

// mymethod.Invoke(obj,null);

Console.ReadLine();

}

}

}

实际上不管你是.exe或dll 组成的程序集 都可以被加载

自定义绑定

除了由编译器隐式地用来进行晚期绑定之外(指virtual方法,接口等相关实现的绑定),反射还可以在代码中显式地用来完成晚期绑定。 公共语言运行库支持多种编程语言,但这些语言的绑定规则各不相同。在早期绑定的情况下,代码生成器可以完全控制此绑定。但是,当通过反射进行晚期绑定时,必须用自定义绑定来控制绑定。Binder 类提供了对成员选择和调用的自定义控制。

利用自定义绑定,您可以在运行时加载程序集,获取有关该程序集中类型的信息,然后对该类型调用方法或访问该类型的字段或属性。如果您在编译时(例如当对象类型依赖于用户输入时)不知道对象的类型,就可以使用这种方法。

using System;

namespace dy_loaddll

{

public class HelloWorld

{

// Constant Hello World string.

private const String m_helloWorld = "Hello World";

// Default public constructor.

public HelloWorld()

{

}

// Print "Hello World" plus the passed text.

public void PrintHello(String txt)

{

// Output to the Console.

Console.WriteLine(m_helloWorld + " " + txt);

}

}

}

编辑生成dy_loaddll.dll,与 LoadExe.exe在同一的目录下面。

using System;

using System.Reflection;

namespace Use_dy_Load_Assembly

{

class LoadExe

{

[STAThread]

static void Main(string[] args)

{

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

Assembly a = Assembly.LoadFrom("dy_loaddll.dll");

Type [] types2 = a.GetTypes();

foreach (Type t in types2)

{

Console.WriteLine (t.FullName);

}

//Get the type to use.

// Type myType = a.GetType(""); 这样写老是出错因为上面的dll加了命名空间

Type myType = a.GetType("dy_loaddll.HelloWorld");

// Type myType = a.GetType("HelloWorld");

Console.WriteLine (myType.FullName);

// //Get the method to call.

MethodInfo printMethod = myType.GetMethod("PrintHello");

// Create an instance of the HelloWorld class.

Object obj = Activator.CreateInstance(myType);

// Create the args array.

Object[] test = new Object[1];

// Set the arguments.

test[0] = "From CSharp Late Bound";

// Invoke the PrintHello method.

printMethod.Invoke(obj, test);

Console.ReadLine();

}

}

}

当然我们不禁要问题,如果方法重载,如果是属性。又该怎么办??

/查相关的msdn,

ms-help://MS.VSCC/MS.MSDNVS.2052/cpguide/html/cpcondynamicallyloadingusingtypes.htm#cpcondynamicallyloadingusingtypes

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有