加载DLL到一个单独的AppDomain [英] Loading DLLs into a separate AppDomain

查看:159
本文介绍了加载DLL到一个单独的AppDomain的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要动态加载一个或多个DLL,让他们用不同的安全或basepath不是我主要的应用程序运行。如何加载这些DLL到一个单独的AppDomain,并从中实例化对象?

I want to load one or more DLLs dynamically so that they run with a different security or basepath than my main application. How do I load these DLLs into a separate AppDomain and instantiate objects from them?

推荐答案

更​​具体

AppDomain domain = AppDomain.CreateDomain("New domain name");
//Do other things to the domain like set the security policy

string pathToDll = @"C:\myDll.dll"; //Full path to dll you want to load
Type t = typeof(TypeIWantToLoad);
TypeIWantToLoad myObject = (TypeIWantToLoad)domain.CreateInstanceFromAndUnwrap(pathToDll, t.FullName);

如果一切去正确(无例外抛出),你现在有TypeIWantToLoad加载到您的新域的一个实例。你有实例实际上是一个代理(因为实际的目标是在新的领域),但你可以使用它就像你的普通对象。

If all that goes properly (no exceptions thrown) you now have an instance of TypeIWantToLoad loaded into your new domain. The instance you have is actually a proxy (since the actual object is in the new domain) but you can use it just like your normal object.

注:据我所知TypeIWantToLoad必须从MarshalByRefObject继承的

Note: As far as I know TypeIWantToLoad has to inherit from MarshalByRefObject.

这篇关于加载DLL到一个单独的AppDomain的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆