.NET DLL references/dependency checking utility

扶醉桌前 提交于 2019-11-29 00:38:41

问题


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.

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.

I checked cygwin's ldd and Depends.exe, but they don't seem to show the dlls from other projects, but only system dlls.


回答1:


Yes: ildasm.exe. It is installed with the SDK.

Should be in a path like: C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin




回答2:


I like to use AsmSpy for 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




回答3:


You can use the provided ildasm.exe, or if you want something a little bit more powerfull, you can try:

Reflector

Even better, try the free:

dotPeek

Greetings,




回答4:


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.

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.




回答5:


This code will do a fairly good job at finding all the references. "temp" will be a dump of all the references it traced.

    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);
            }
        }
    }


来源:https://stackoverflow.com/questions/8750018/net-dll-references-dependency-checking-utility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!