如何确定 .NET 应用程序的依赖项?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/227886/
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
How do I determine the dependencies of a .NET application?
提问by Brian Stewart
How do I determine the dependencies of a .NET application? Does Dependency Walkerwork with managed apps? I've downloaded the latest and tried profiling the app, but it just exits without much of an explanation. If it doesn't work with .NET, then is there some other tool that would help me debug a run-time DLL loading issue?
如何确定 .NET 应用程序的依赖项?Dependency Walker是否适用于托管应用程序?我已经下载了最新的并尝试分析该应用程序,但它只是退出而没有太多解释。如果它不适用于 .NET,那么是否有其他工具可以帮助我调试运行时 DLL 加载问题?
采纳答案by Orion Edwards
Dependency walker works on normal win32 binaries. All .NET dll's and exe's have a small stub header part which makes them look like normal binaries, but all it basically says is "load the CLR" - so that's all that dependency walker will tell you.
Dependency walker 适用于普通的 win32 二进制文件。所有 .NET dll 和 exe 都有一个小的存根标头部分,这使它们看起来像普通的二进制文件,但它基本上说的是“加载 CLR”——这就是依赖遍历器会告诉你的全部内容。
To see which things your .NET app actually relies on, you can use the tremendously excellent .NET reflectorfrom Red Gate. (EDIT: Note that .NET Reflector is now a paid product. ILSpyis free and open source and very similar.)
要查看您的 .NET 应用程序实际依赖哪些内容,您可以使用来自 Red Gate的非常出色的.NET 反射器。(编辑:请注意,.NET Reflector 现在是付费产品。ILSpy是免费和开源的,非常相似。)
Load your DLL into it, right click, and chose 'Analyze' - you'll then see a "Depends On" item which will show you all the other dll's (and methods inside those dll's) that it needs.
将您的 DLL 加载到其中,右键单击并选择“分析” - 然后您将看到一个“取决于”项目,该项目将向您显示它需要的所有其他 dll(以及这些 dll 中的方法)。
It can sometimes get trickier though, in that your app depends on X dll, and X dll is present, but for whatever reason can't be loaded or located at runtime.
但有时它会变得更棘手,因为您的应用程序依赖于 X dll,并且 X dll 存在,但无论出于何种原因无法在运行时加载或定位。
To troubleshoot those kinds of issues, Microsoft have an Assembly Binding Log Viewerwhich can show you what's going on at runtime
为了解决这些类型的问题,Microsoft 有一个程序集绑定日志查看器,它可以显示运行时发生的情况
回答by Alexander van Trijffel
I find the small utility AsmSpyan invaluable tool to for resolving issues with loading assemblies. It lists all assembly references of managed assemblies including assembly versions.
我发现小实用程序AsmSpy是解决加载程序集问题的宝贵工具。它列出了托管程序集的所有程序集引用,包括程序集版本。
Run it in a command prompt in the directory of the .dllwith the following arguments:
.dll使用以下参数在 目录中的命令提示符下运行它:
asmspy . all
Install it quickly with Chocolatey:
使用 Chocolatey 快速安装:
choco install asmspy
回答by Jim
Open the assembly file in ILDASM and look @ the .assembly extern in the MANIFEST
在ILDASM中打开程序集文件并在MANIFEST中查看@.assembly extern
回答by Patrick from NDepend team
To browse .NET code dependencies, you can use the capabilities of the tool NDepend. The tool proposes:
要浏览 .NET 代码依赖项,您可以使用工具 NDepend 的功能。该工具建议:
- a dependency graph
- a dependency matrix,
- and also some C# LINQ queriescan be edited (or generated) to browse dependencies.
- 一个依赖图
- 一个相关矩阵,
- 还可以编辑(或生成)一些C# LINQ 查询来浏览依赖项。
For example such query can look like:
例如,此类查询可能如下所示:
from m in Methods
let depth = m.DepthOfIsUsing("NHibernate.NHibernateUtil.Entity(Type)")
where depth >= 0 && m.IsUsing("System.IDisposable")
orderby depth
select new { m, depth }
And its result looks like: (notice the code metric depth, 1 is for direct callers, 2 for callers of direct callers...) (notice also the Export to Graph button to export the query result to a Call Graph)
它的结果看起来像:(注意代码度量深度,1 是直接调用者,2 是直接调用者的调用者......)(还要注意导出到图表按钮将查询结果导出到调用图)
The dependency graph looks like:
依赖图如下所示:
The dependency matrix looks like:
依赖矩阵如下所示:
The dependency matrix is de-factoless intuitive than the graph, but it is more suited to browse complex sections of code like:
依赖矩阵实际上不如图形直观,但它更适合浏览复杂的代码部分,例如:
Disclaimer: I work for NDepend
免责声明:我为 NDepend 工作
回答by Colonel Panic
You don't need to download and install shareware apps or tools. You can do it programitically from .NET using Assembly.GetReferencedAssemblies()
您无需下载和安装共享软件应用程序或工具。您可以使用 .NET 以编程方式执行此操作Assembly.GetReferencedAssemblies()
Assembly.LoadFile(@"app").GetReferencedAssemblies()
回答by Alexander O'Mara
If you are using the Mono toolchain, you can use the monodisutilitywith the --assemblyrefargument to list the dependencies of a .NET assembly. This will work on both .exeand .dllfiles.
如果您使用的是 Mono 工具链,则可以使用带有参数的monodis实用程序--assemblyref来列出 .NET 程序集的依赖项。这将适用于.exe和.dll文件。
Example usage:
用法示例:
monodis --assemblyref somefile.exe
Example output (.exe):
示例输出 (.exe):
$ monodis --assemblyref monop.exe
AssemblyRef Table
1: Version=4.0.0.0
Name=System
Flags=0x00000000
Public Key:
0x00000000: B7 7A 5C 56 19 34 E0 89
2: Version=4.0.0.0
Name=mscorlib
Flags=0x00000000
Public Key:
0x00000000: B7 7A 5C 56 19 34 E0 89
Example output (.dll):
示例输出 (.dll):
$ monodis --assemblyref Mono.CSharp.dll
AssemblyRef Table
1: Version=4.0.0.0
Name=mscorlib
Flags=0x00000000
Public Key:
0x00000000: B7 7A 5C 56 19 34 E0 89
2: Version=4.0.0.0
Name=System.Core
Flags=0x00000000
Public Key:
0x00000000: B7 7A 5C 56 19 34 E0 89
3: Version=4.0.0.0
Name=System
Flags=0x00000000
Public Key:
0x00000000: B7 7A 5C 56 19 34 E0 89
4: Version=4.0.0.0
Name=System.Xml
Flags=0x00000000
Public Key:
0x00000000: B7 7A 5C 56 19 34 E0 89
回答by Ramesh
Enable assembly binding logging set the registry value EnableLog in HKLM\Software\Microsoft\Fusion to 1. Note that you have to restart your application (use iisreset) for the changes to have any effect.
启用程序集绑定日志将 HKLM\Software\Microsoft\Fusion 中的注册表值 EnableLog 设置为 1。请注意,您必须重新启动应用程序(使用 iisreset)才能使更改生效。
Tip:Remember to turn off fusion logging when you are done since there is a performance penalty to have it turned on.
提示:请记住在完成后关闭融合日志记录,因为打开它会降低性能。
回答by Lloyd
It's funny I had a similar issue and didn't find anything suitable and was aware of good old Dependency Walker so in the end I wrote one myself.
有趣的是,我有一个类似的问题,没有找到任何合适的东西,并且知道很好的老依赖沃克,所以最后我自己写了一个。
This deals with .NET specifically and will show what references an assembly has (and missing) recursively. It'll also show native library dependencies.
这专门处理 .NET,并将以递归方式显示程序集具有(和缺少)的引用。它还将显示本机库依赖项。
It's free (for personal use) and available here for anyone interested: www.netdepends.com
它是免费的(供个人使用),任何感兴趣的人都可以在此处获得:www.netdepends.com
Feedback welcome.
欢迎反馈。
回答by mhenry1384
ChkAsm will show you all the dependencies of a particular assembly at once, including the versions, and easily let you search for assemblies in the list. Works much better for this purpose than ILSpy (http://ilspy.net/), which is what I used to use for this task.
ChkAsm 将一次向您显示特定程序集的所有依赖项,包括版本,并让您轻松地在列表中搜索程序集。为此目的,它比 ILSpy ( http://ilspy.net/)要好得多,这是我用来完成这项任务的。
回答by Richard Morgan
Another handy Reflector add-in that I use is the Dependency Structure Matrix. It's really great to see what classes use what. Plus it's free.
我使用的另一个方便的 Reflector 插件是Dependency Structure Matrix。很高兴看到什么类使用什么。另外它是免费的。


