如何利用AppDomains [英] How to leverage AppDomains

查看:169
本文介绍了如何利用AppDomains的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要开发一个实用程序程序集,它提供可以接受(某些其他DLL)路径的函数,并使用该DLL上的反射来查看它是否存在包含或使用某些类。

I need to develop a utility assembly that offers functions that can accept the path (of some other DLL) and use reflection on that DLL to see if it contains or uses certain classes.

在调用者AppDomain中执行此操作会导致由于与已加载的程序集发生冲突而无法加载DLL时出现问题(即使我们仅使用反射加载DLL)。

Doing this within the callers AppDomain leads to problems when we can't load the DLL due to conflicts with already loaded assemblies (even if we use reflection only load on the DLL).

我无法理解创建AppDomain并与之交互,以便它可以代表调用者进行加载,也许只需在完成后处理AppDomain 。

I can't quite figure out to create and interact with an AppDomain so that it can do the load on behalf of the caller, perhaps simply disposing the AppDomain after its done.

所以从概念上讲我想要一个方法,比如:bool UsesType(字符串DLL,Type SomeType)在内部创建一个AppDomain,加载DLL做反射并返回true或false并关闭/关闭创建的AppDomain。 (它可能需要接受一个
类型的名称,因为某些AppDoman中必须存在类型本身。)

So conceptually I want a method, say: bool UsesType (string DLL, Type SomeType) that internally creates an AppDomain, that loads the DLL does the reflection and returns true or false and closes/shuts that created AppDomain. (It may need to accept a type name because utltimately the type itself must exist in some AppDoman).

这些代码一般会如何看待?

How would such code look in general?

Thx

推荐答案

我建​​议另一种方法:首先,创建自己的程序集,执行所有操作,以通过反射检查
其他程序集。然后创建一个AppDomain并在AppDomain中加载您自己的程序集。通过这种方式,您可以确切地知道要加载的内容以及它公开的方法,以便您可以从AppDomain外部调用它。然后,
程序集可以加载其他程序集,它们将被加载到自己的AppDomain中。完成后,只需卸载AppDomain,这将卸载所有程序集,包括您自己的第一个程序集。
I'd suggest another way to do it: First, create your own assembly that performs all the operations to examine other assemblies via reflection. Then create an AppDomain and load your own assembly inside the AppDomain. In this way, you know exactly what you are loading and which methods it exposes, so that you can call it from outside the AppDomain. Then, your assembly can load other assemblies and they will be loaded inside its own AppDomain. When you are done, just unload the AppDomain, and this will unload all the assemblies, including your own first assembly.


这篇关于如何利用AppDomains的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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