C# 找出所有 DLL 的依赖关系?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15113352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:06:16  来源:igfitidea点击:

Find out dependencies of all DLLs?

c#.net-assemblyassemblies

提问by Abhijeet

I have a collection of DLLs(say 20). How do I find out all the DLLs on which one specific DLL (say DLL A) is depending upon?

我有一组 DLL(比如 20 个)。如何找出一个特定 DLL(例如 DLL A)所依赖的所有 DLL?

采纳答案by Jon Skeet

If you mean programmatically, use Assembly.GetReferencedAssemblies.

如果您的意思是以编程方式,请使用Assembly.GetReferencedAssemblies.

You can use that recursively to find all the assemblies you need. (So you find the dependencies of X, then the dependencies of the dependencies, etc.)

您可以递归地使用它来查找您需要的所有程序集。(所以你找到X的依赖,然后是依赖的依赖等等)

回答by Aniket Inge

You can use dependency walker http://www.dependencywalker.comto figure this out. Take note on the difference between x32 and x64 though.

您可以使用dependency walker http://www.dependencywalker.com来解决这个问题。不过请注意 x32 和 x64 之间的区别。

Dependency Walker is a free utility that scans any 32-bit or 64-bit Windows module (exe, dll, ocx, sys, etc.) and builds a hierarchical tree diagram of all dependent modules.

Dependency Walker 是一款免费实用程序,可扫描任何 32 位或 64 位 Windows 模块(exe、dll、ocx、sys 等)并构建所有依赖模块的层次树图。

回答by Lorenzo Dematté

Since the question is tagged "C#", I would assume you are talking about managed dlls (assemblies). In that case, dependencywalker is not useful. If you want to do that with a program, good ones are dotPeek by JetBrians and Reflector by RedGate. Or you can even use the object inspector in Visual Studio.

由于问题被标记为“C#”,我假设您在谈论托管 dll(程序集)。在这种情况下,dependencywalker 没有用。如果你想用一个程序来做到这一点,好的是 JetBrians 的 dotPeek 和 RedGate 的 Reflector。或者您甚至可以使用 Visual Studio 中的对象检查器。

However, it can be a long process and cumbersome too. I would write a short C# program/F# script that uses Assembly.GetReferencedAssemblies, as Jon mentioned.

然而,这可能是一个漫长的过程,也很麻烦。我会写一个简短的 C# 程序/F# 脚本,使用Assembly.GetReferencedAssemblies,正如 Jon 提到的。

If instead you want to examine native DLLs dependencies with a program (C# code), you have to walk the examine the PE file (the MS dll and exe file format) and its IAT (import address table). Not easy, but not impossible...

相反,如果您想使用程序(C# 代码)检查本机 DLL 依赖项,则必须检查 PE 文件(MS dll 和 exe 文件格式)及其 IAT(导入地址表)。不容易,但也不是不可能……

I would start here on MSDNand hereto understand PE sections, and use a managed library to read it (there are many, including some from the Mono project (I'm thinking of Cecil, it should work with native binaries too); in the past I have used this onefrom the good John Gough.

我将从MSDN这里开始了解 PE 部分,并使用托管库来阅读它(有很多,包括来自 Mono 项目的一些(我正在考虑Cecil,它也应该与本机二进制文件一起使用);在过去我用过这个来自优秀的约翰高夫。

回答by Alexander van Trijffel

For .NET assemblies, a terrific tool to view the assemblies an assembly is dependent on is AsmSpy.

对于 .NET 程序集,查看程序集依赖的程序集的绝佳工具是AsmSpy

回答by kayleeFrye_onDeck

All answer credit goes to previous authors for the usage of Assembly.GetReferencedAssemblies. This is just a write-and-forget C# console app that works solely for .NET assemblies. return 0on assemblies you were able to check, and when successful, outputs them to STDOUT. Everything else will return 1and print some kind of error output. You can grab the gist here.

所有答案都归功于以前的作者使用Assembly.GetReferencedAssemblies. 这只是一个写完即忘的 C# 控制台应用程序,仅适用于 .NET 程序集。return 0在您能够检查的程序集上,成功后,将它们输出到 STDOUT。其他一切都会return 1并打印某种错误输出。你可以在这里抓住要点。

using System;
using System.Reflection;
using System.IO;
namespace DotNetInspectorGadget
{
    class DotNetInspectorGadget 
    {
        static int Main(string[] args) 
        {
          if(args.GetLength(0) < 1)
          {
            Console.WriteLine("Add a single parameter that is your" +
            " path to the file you want inspected.");
            return 1;
          }
          try {
                var assemblies = Assembly.LoadFile(@args[0]).GetReferencedAssemblies();

                if (assemblies.GetLength(0) > 0)
                {
                  foreach (var assembly in assemblies)
                  {
                    Console.WriteLine(assembly);
                  }
                  return 0;
                }
          }
          catch(Exception e) {
            Console.WriteLine("An exception occurred: {0}", e.Message);
            return 1;
          } finally{}

            return 1;
        }
    }
}

Usage:

用法:

call %cd%\dotnet_inspector_gadget.exe C:\Windows\Microsoft.NET\assembly\GAC_64\Microsoft.ConfigCI.Commands\v4.0_10.0.0.0__31bf3856ad364e35\Microsoft.ConfigCI.Commands.dll

Output:

输出:

mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a

回答by Louis Somers

If you want the DLL's (the files) then, Assembly.GetReferencedAssemblieswill also return the .Net Framework assemblies.

如果您想要 DLL(文件),Assembly.GetReferencedAssemblies则还将返回 .Net Framework 程序集。

Here is a simple code snippet that will get the dll's it can find in the current directory (and also include some other related files):

这是一个简单的代码片段,它将获取它可以在当前目录中找到的 dll(还包括一些其他相关文件):

private readonly string[] _extensions = { ".dll", ".exe", ".pdb", ".dll.config", ".exe.config" };

private string[] GetDependentFiles(Assembly assembly)
{
    AssemblyName[] asm = assembly.GetReferencedAssemblies();
    List<string> paths = new List<string>(asm.Length);
    for (int t = asm.Length - 1; t >= 0; t--)
    {
        for (int e = _extensions.Length - 1; e >= 0; e--)
        {
            string path = Path.GetFullPath(asm[t].Name + _extensions[e]);
            if (File.Exists(path)) paths.Add(path);
        }
    }

    return paths.ToArray();
}

You can call it like so: MessageBox.Show(string.Join("\r\n", GetDependentFiles(Assembly.GetEntryAssembly())));

你可以这样称呼它: MessageBox.Show(string.Join("\r\n", GetDependentFiles(Assembly.GetEntryAssembly())));