简体   繁体   English

从System.Type在AppDomain中创建实例

[英]Creating an Instance in an AppDomain from a System.Type

I am trying to create an Instance in an AppDomain. 我正在尝试在AppDomain中创建一个实例。 So I can create a seperate AppDomain for each of my plugins and dispose of them correctly when I close them using the Unload method. 因此,我可以为每个插件创建一个单独的AppDomain,并在使用Unload方法关闭它们时正确处理它们。

So far I have an IEnumerator of all my plugins and I cycle through them executing the following code. 到目前为止,我的所有插件都有一个IEnumerator,并通过它们循环执行以下代码。

Type temp = (Type)enumerator.Current;
AppDomain ad = AppDomain.CreateDomain(temp.FullName);
object obj = ad.CreateInstance(temp.Assembly.FullName, temp.FullName);

But it crashes on the third line saying that it could not load file or assembly. 但是它在第三行崩溃,表明它无法加载文件或程序集。

Previously I used: 以前我用过:

object obj = Activator.CreateInstance((Type)enumerator.Current);

but found I could not dispose of the plugins correctly by calling base.dispose() meaning service calls were left open and code would continue to execute when the service returned. 但是发现我无法通过调用base.dispose()来正确处理这些插件,这意味着服务调用保持打开状态,当服务返回时代码将继续执行。

Is it possible to create an app domain from a type? 是否可以根据类型创建应用程序域?

Ok so I found my mistake. 好的,所以我发现了我的错误。 It should have read: 它应该显示为:

object obj = ad.CreateInstanceFrom(temp.Assembly.Location, temp.FullName);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM