加载DLL到一个单独的AppDomain加载、DLL、AppDomain

由网友(为你褪去一身骄傲)分享简介:我要动态加载一个或多个DLL,让他们用不同的安全或basepath不是我主要的应用程序运行。如何加载这些DLL到一个单独的AppDomain,并从中实例化对象?I want to load one or more DLLs dynamically so that they run with a different s...

我要动态加载一个或多个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.

阅读全文

相关推荐

最新文章