C# 反射:在解决方案中获取*所有* 活动程序集? [英] C# Reflection: Get *all* active assemblies in a solution?

查看:31
本文介绍了C# 反射:在解决方案中获取*所有* 活动程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题:

我有 2 个项目 - 一个通用"项目,其行为就像一个包含各种支持代码的库,以及在许多调用中使用所述项目的实际程序.我们将这些项目称为普通"项目.和程序".它们都在同一个解决方案中.

I have 2 projects - one 'common' projects with acts like a library with all kinds of support code, and the actual program that uses said project in many of its calls. We'll call these projects "Common" and "Program". They are both in the same solution.

在Common"中,我有一个用于常见反射任务的类,比如创建一个实例.如果我调用 GetExecutingAssembly,它会获取所有的Common"类型,但是当我使用 GetEntryAssembly 时,我得到了程序"类型.

Within "Common", I have a class for common reflection tasks, like creating an instance. If I call GetExecutingAssembly, it gets all the "Common" Types, however when I use GetEntryAssembly I get the "Program" types.

虽然我当然可以编辑代码以使用 2 组 asm,但我担心解决方案中不止 2 个项目的情况 - 比如说 5 个(不知道为什么,但让我们暂时去那里),恐怕调用 GetExecutingAssembly 和 GetEntryAssembly 不会得到整个程序中的所有类型.

While I certainly could edit the code to work with 2 sets of asm, I'm afraid of a situation where there are more than just 2 projects in the solution - lets say 5 (don't know why, but lets just go there for now), and I'm afraid that calling GetExecutingAssembly and GetEntryAssembly will not get all the Types in the entire program.

我还能做些什么来获得解决方案中的所有类型?

Is there something else that i can do to get all the Types in a solution?

推荐答案

Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

这将获取当前 AppDomain 中所有加载的程序集.

This will get all of the loaded assemblies in the current AppDomain.

如评论中所述,可以生成多个 AppDomain,在这种情况下,每个 AppDomain 都可以有自己的程序集.这样做的直接好处是您可以通过卸载包含的 AppDomain 来卸载程序集.

As noted in the comments, it's possible to spawn multiple AppDomains, in which case each can have its own assemblies. The immediate advantage to doing so is that you can unload Assemblies by unloading the containing AppDomain.

这篇关于C# 反射:在解决方案中获取*所有* 活动程序集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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