.NET DLL 引用/依赖性检查实用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8750018/
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
.NET DLL references/dependency checking utility
提问by prosseek
With Visual Studio, I can see the dll references as the attached picture as an example. With this reference information, I can open the (from the example) Composition project to find all the other references on and on to get all the reference file names.
使用Visual Studio,我可以看到dll引用作为附加图片作为示例。使用此参考信息,我可以打开(来自示例)Composition 项目以查找所有其他参考以获取所有参考文件名。
Is there any utility that does this job automatically? I mean, given an .NET assembly, it checks all the references/dependencies recursively to give the names of DLLs.
是否有任何实用程序可以自动完成这项工作?我的意思是,给定一个 .NET 程序集,它会递归地检查所有引用/依赖项以给出 DLL 的名称。
I checked cygwin's ldd and Depends.exe, but they don't seem to show the dlls from other projects, but only system dlls.
我检查了 cygwin 的 ldd 和 Depends.exe,但它们似乎没有显示来自其他项目的 dll,而只显示系统 dll。


回答by competent_tech
Yes: ildasm.exe. It is installed with the SDK.
是:ildasm.exe。它与 SDK 一起安装。
Should be in a path like: C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin
应该在如下路径中:C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin
回答by Richard Edwards
I like to use AsmSpyfor this type of check. There is a single exe to download and then I put it into my tools folder for easy access. https://github.com/mikehadlow/AsmSpy
我喜欢使用AsmSpy进行此类检查。有一个 exe 可供下载,然后我将其放入我的工具文件夹中以便于访问。 https://github.com/mikehadlow/AsmSpy
回答by Gonza Oviedo
回答by Arrow
This code will do a fairly good job at finding all the references. "temp" will be a dump of all the references it traced.
这段代码将在查找所有引用方面做得很好。“temp”将是它跟踪的所有引用的转储。
private void PerformReferenceAnalysis()
{
StringBuilder builder = new StringBuilder();
HashSet<string> loadedAssemblies = new HashSet<string>();
PerformReferenceAnalysis(System.Reflection.Assembly.GetExecutingAssembly(), builder, string.Empty, loadedAssemblies);
string temp = builder.ToString();
}
private void PerformReferenceAnalysis(System.Reflection.Assembly assembly, StringBuilder builder, string leadingWhitespace, HashSet<string> loadedAssemblies)
{
if (builder.Length > 0)
{
builder.AppendLine();
}
builder.Append(leadingWhitespace + assembly.FullName);
System.Reflection.AssemblyName[] referencedAssemblies = assembly.GetReferencedAssemblies();
foreach (System.Reflection.AssemblyName assemblyName in referencedAssemblies)
{
if (loadedAssemblies.Contains(assemblyName.Name))
{
continue;
}
loadedAssemblies.Add(assemblyName.Name);
System.Reflection.Assembly nextAssembly;
try
{
nextAssembly = System.Reflection.Assembly.ReflectionOnlyLoad(assemblyName.FullName);
}
catch (Exception)
{
try
{
nextAssembly = System.Reflection.Assembly.ReflectionOnlyLoad(assemblyName.Name);
}
catch (Exception)
{
nextAssembly = null;
}
}
if (nextAssembly != null)
{
PerformReferenceAnalysis(nextAssembly, builder, leadingWhitespace + "| ", loadedAssemblies);
}
}
}
回答by Ciaran Keating
I don't know of any tools to do this automatically for you, but I can outline the steps any tool (or you) would have to follow.
我不知道有什么工具可以为您自动执行此操作,但我可以概述任何工具(或您)必须遵循的步骤。
Each assembly contains a manifest. The manifest lists the names and versions of all other assemblies that the current assembly depends upon. At its simplest you'd need to follow this trail recursively.
每个程序集都包含一个清单。清单列出了当前程序集所依赖的所有其他程序集的名称和版本。在最简单的情况下,您需要递归地跟踪此路径。
There's no correct way to tell you the filenames of the referenced assemblies. References are stored in the manifest as assembly names (name, version, culture, etc.) and not as filenames. When the .NET runtime needs to load a referenced assembly it uses various searches to find it, and the result of these searches may vary from environment to environment. Of course, this might not be a problem for you if you're just looking for the assemblies on your development machine, for example.
没有正确的方法可以告诉您所引用程序集的文件名。引用作为程序集名称(名称、版本、区域性等)而不是文件名存储在清单中。当 .NET 运行时需要加载引用的程序集时,它会使用各种搜索来查找它,并且这些搜索的结果可能因环境而异。当然,例如,如果您只是在开发机器上寻找程序集,这对您来说可能不是问题。
Techniques for resolving assembly references include searching any assemblies already loaded, looking in the Global Assembly Cache, looking in the application directory, redirecting based on application configuration files or publisher policies, and others. Google for the article "How the Runtime Locates Assemblies" in MSDN for more details. In addition, your application can register to do its own reference resolution by handling the System::AppDomain::AssemblyResolve event.
解析程序集引用的技术包括搜索任何已加载的程序集、查看全局程序集缓存、查看应用程序目录、基于应用程序配置文件或发布者策略进行重定向等。谷歌搜索 MSDN 中的文章“运行时如何定位程序集”以获取更多详细信息。此外,您的应用程序可以注册以通过处理 System::AppDomain::AssemblyResolve 事件来执行自己的引用解析。

