获取依赖程序集?

发布于 2024-12-26 09:35:18 字数 130 浏览 1 评论 0原文

有没有办法获取依赖于给定程序集的所有程序集?

伪:

Assembly a = GetAssembly();
var dependants = a.GetDependants();

Is there a way to get all assemblies that depend on a given assembly?

Pseudo:

Assembly a = GetAssembly();
var dependants = a.GetDependants();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一场信仰旅途 2025-01-02 09:35:18

如果您希望从当前应用程序域中查找依赖程序集,您可以使用类似于下面定义的 GetDependentAssemblies 函数:

private IEnumerable<Assembly> GetDependentAssemblies(Assembly analyzedAssembly)
{
    return AppDomain.CurrentDomain.GetAssemblies()
        .Where(a => GetNamesOfAssembliesReferencedBy(a)
                            .Contains(analyzedAssembly.FullName));
}

public IEnumerable<string> GetNamesOfAssembliesReferencedBy(Assembly assembly)
{
    return assembly.GetReferencedAssemblies()
        .Select(assemblyName => assemblyName.FullName);
}

analyzedAssembly 参数表示您要查找的程序集找到所有的家属。

If you wish to find the dependent assemblies from the current application domain, you could use something like the GetDependentAssemblies function defined below:

private IEnumerable<Assembly> GetDependentAssemblies(Assembly analyzedAssembly)
{
    return AppDomain.CurrentDomain.GetAssemblies()
        .Where(a => GetNamesOfAssembliesReferencedBy(a)
                            .Contains(analyzedAssembly.FullName));
}

public IEnumerable<string> GetNamesOfAssembliesReferencedBy(Assembly assembly)
{
    return assembly.GetReferencedAssemblies()
        .Select(assemblyName => assemblyName.FullName);
}

The analyzedAssembly parameter represents the assembly for which you want to find all the dependents.

花间憩 2025-01-02 09:35:18

通过编程,您可以使用 Mono.Cecil 来执行此操作。

像这样的东西(请注意,如果附加了调试器,这将不起作用 - 例如,如果您从 VS 本身内部运行它):

public static IEnumerable<string> GetDependentAssembly(string assemblyFilePath)
{
   //On my box, once I'd installed Mono, Mono.Cecil could be found at: 
   //C:\Program Files (x86)\Mono-2.10.8\lib\mono\gac\Mono.Cecil\0.9.4.0__0738eb9f132ed756\Mono.Cecil.dll
   var assembly = AssemblyDefinition.ReadAssembly(assemblyFilePath);
   return assembly.MainModule.AssemblyReferences.Select(reference => reference.FullName);
}

如果您不需要以编程方式执行此操作,则 NDependReflector 可以为您提供此信息。

Programatically, you can use Mono.Cecil to do this.

Something like this (note this won't work if the debugger is attached - e.g. if you run it from inside VS itself):

public static IEnumerable<string> GetDependentAssembly(string assemblyFilePath)
{
   //On my box, once I'd installed Mono, Mono.Cecil could be found at: 
   //C:\Program Files (x86)\Mono-2.10.8\lib\mono\gac\Mono.Cecil\0.9.4.0__0738eb9f132ed756\Mono.Cecil.dll
   var assembly = AssemblyDefinition.ReadAssembly(assemblyFilePath);
   return assembly.MainModule.AssemblyReferences.Select(reference => reference.FullName);
}

If you don't need to do this programatically, then NDepend or Reflector can give you this information.

御守 2025-01-02 09:35:18

首先定义您的范围,例如:

  1. 我的应用程序的 bin 目录中的所有程序集

  2. 我的应用程序的 bin 目录中的所有程序集 + GAC 中的所有程序集

  3. 世界上任何计算机上的所有程序集。

然后简单地 (*) 迭代范围内的所有程序集,并使用反射来检查它们是否依赖于您的目标程序集。

如果您想要间接和直接引用,则必须对找到的所有程序集进行冲洗和重复。

(*) 如果您的范围是以上 3,则可能不会那么简单。

First define your scope, e.g.:

  1. All assemblies in my application's bin directory

  2. All assemblies in my application's bin directory + all assemblies in the GAC

  3. All assemblies on any machine in the world.

Then simply (*) iterate through all assemblies in your scope, and use reflection to check if they depend on your target assembly.

If you want indirect as well as direct references, you'll have to rinse and repeat for all the assemblies you find.

(*) Might not be quite so simple if your scope is 3 above.

紫轩蝶泪 2025-01-02 09:35:18

我不知道有任何内置的可能性可以在运行时获取依赖项。
所以我认为最简单的解决方案是定义一个扩展方法并使用 this 应用程序中的代码。我一年前使用过一个应用程序本身。但不要使用它的代码。

希望这有帮助。

I'm not aware of any built-in possibility to get dependencies at runtime.
So I think the easiest solution is define an extension method and use code from this application. I used an application itself a years ago. But do not use code of it.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文