如何加载.NET程序集的反思行动,并随后将其卸载? [英] How to load a .NET assembly for reflection operations and subsequently unload it?

查看:247
本文介绍了如何加载.NET程序集的反思行动,并随后将其卸载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个工具来汇报一下我的客户的系统中部署的不同环境和地区的.NET应用程序的信息。

I'm writing a tool to report information about .NET applications deployed across environments and regions within my client's systems.

我想读取值在这些组件组装属性。

I'd like to read the values of assembly attributes in these assemblies.

这可以通过使用 Assembly.ReflectionOnlyLoad 来实现,但即使是这种方法可以使加载的程序集。这里的问题是,我无法加载两个组件具有相同名称从不同的路径,所以我自然无法比拟的部署在不同的系统上相同的应用程序。

This can be achieved using Assembly.ReflectionOnlyLoad, however even this approach keeps the assembly loaded. The issue here is that I cannot load two assemblies that have the same name from different paths, so naturally I can't compare the same application deployed in different systems.

在这一点上我假设该解决方案将涉及使用临时的AppDomain 秒。

At this point I'm assuming the solution will involve using temporary AppDomains.

有人可以详细介绍一下如何组装加载到另一个的AppDomain ,读取它的属性,然后卸载的AppDomain

Can someone detail how to load an assembly into another AppDomain, read the attributes from it and then unload the AppDomain?

这需要以及工作集的文件系统上的那些URL地址。

This needs to work for assemblies on the file system as well as those at URL addresses.

推荐答案

从系统的 MSDN文档。 Reflection.Assembly.ReflectionOnlyLoad(字符串)

在只反射上​​下文中没有   不同于其他上下文中。   被装载到组件   上下文只能通过卸载   卸载应用程序域。

The reflection-only context is no different from other contexts. Assemblies that are loaded into the context can be unloaded only by unloading the application domain.

所以,恐怕要卸载的组件卸载应用程序域的唯一途径。 要创建一个新的AppDomain和负载装配到其中:

So, I am afraid the only way to unload an assembly is unloading the application domain. To create a new AppDomain and load assemblies into it:

public void TempLoadAssembly()
{
    AppDomain tempDomain = AppDomain.CreateDomain("TemporaryAppDomain");
    tempDomain.DoCallBack(LoaderCallback);
    AppDomain.Unload(tempDomain);
}

private void LoaderCallback()
{
    Assembly.ReflectionOnlyLoad("YourAssembly");
    // Do your stuff here
}

这篇关于如何加载.NET程序集的反思行动,并随后将其卸载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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