寻找一个自由和开放源码工具来显示一个.NET程序集链接依赖源码、链接、自由、工具

2023-09-06 06:45:36 作者:慰我彷徨

谁能推荐一个工具(最好FOSS),可以分析一个.NET程序集,并列出其依赖关系?

我是后是给定一个.NET的exe文件可以产生一个报告,显示我的.NET程序集(名称,版本,强名称,位置/ GAC),其连接到的工具;为了当部署失败,应用程序不会运行,以协助诊断。

熔断器日志查看器可以帮助做到这一点时,它已经走了错了,但它有点笨重。我宁愿能分析大会积极主动,看看它需要在运行时,所以我可以证实他们是present。我看过各种反射器的工具,但惠斯特中所有的链接集这些列表类,并把它们的名字不似乎预示强大的名称,版本等。

分辨率

三江源所有的非常有用的反馈。虽然ILDASM提供我所需要的自由(和我应该记得),其未使用非常优雅,当你想要的是应用x.exe的依赖关系。

.net反射是不是免费的了也不是NDepend的(我的使用方面),所以那些不得不被拒绝。

所以我创造了什么是可能是世界上最简单的控制台应用程序,它需要一个文件名,并打印扶养名称和verions基于艾云的回答这是相当suffient满足我的需求。

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用System.IO;
使用的System.Reflection;

命名空间AssemblyDependencyViewer
{
类节目
{
    静态无效的主要(字串[] args)
    {
        如果(File.Exists(参数[0]))
        {
            DumpFileReferences(参数[0]);
        }
        其他
        {
            VAR DIR =新的DirectoryInfo(参数[0]);
            如果(dir.Exists)
            {
                var中的文件= dir.GetFiles()
                    。凡((S)=>
                    {
                        返程(s.Extension ==.EXE)|| (s.Extension ==.DLL);
                    })
                    .OrderBy((S2)=>
                    {
                        返回s2.Name;
                    })
                    ;

                的foreach(在文件var文件)
                {
                    Console.WriteLine(的String.Format(===文件{0} ====,file.FullName));
                    DumpFileReferences(file.FullName);
                }
            }
        }

        Console.ReadKey(假);
    }

    私有静态无效DumpFileReferences(字符串文件名)
    {
        尝试
        {
            VAR组装= Assembly.ReflectionOnlyLoadFrom(文件名);
            VAR裁判= assembly.GetReferencedAssemblies();

            的foreach(变种中的x refs.OrderBy((S)=>
            {
                返回s.Name;
            }))
            {
                PrintFilename(X);
            }
        }
        赶上(BadImageFormatException BIF)
        {
            Console.WriteLine(bif.Message);
        }
        赶上(FileLoadException FL)
        {
            Console.WriteLine(fl.Message);
        }
    }

    私有静态无效PrintFilename(的AssemblyName X)
    {
        如果(x.Name.StartsWith(Apdcomms))
        {
            Console.ForegroundColor = ConsoleColor.Magenta;
        }
        Console.WriteLine(的String.Format({1,-10} {0},x.Name,x.Version));
        Console.ResetColor();
    }
}
}
 

解决方案

这并不是说很难创建一个这样的工具自己:

  VAR组装= Assembly.ReflectionOnlyLoadFrom(@......文件......);

VAR裁判= assembly.GetReferencedAssemblies();
 

裁判随后将包含直接引用。

还在到处找代码对比工具,六星教育告诉你用这7个就够了

Can anyone recommend a tool (ideally FOSS) that can analyse an .NET assembly and list its dependencies?

What I am after is a tool that given a .NET exe file can produce a report showing me the .NET assemblies (name, version, strong name, location/GAC) its is linked to; in order to assist diagnosis when a deployment fails and the application wont run.

The fuse-log viewer helps do this when its already gone wrong but its a bit clunky. I would rather be able to analyse the assembly pro-actively and see what it will need at runtime so I can validate they are present. I have looked at various reflector tools, but whist these list classes in all the linked assemblies, and names non of them seem to indicate strong names, versions etc.

Resolution

Thankyou all for the very useful feedback. Whilst ILDASM provides what I need for free (and I should have remembered that) its not very elegant in use when all you want is application "x.exe"'s dependancies.

.NET Reflector is not free anymore and neither is NDepend (for my usage terms) so those had to be rejected.

So I have created what is possibly the world's simplest console application which takes a filename and prints the dependancy names and verions based on Arve's answer which is quite suffient for my needs.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace AssemblyDependencyViewer
{
class Program
{
    static void Main(string[] args)
    {
        if (File.Exists(args[0]))
        {
            DumpFileReferences(args[0]);
        }
        else
        {
            var dir = new DirectoryInfo(args[0]);
            if (dir.Exists)
            {
                var files = dir.GetFiles()
                    .Where((s) =>
                    {
                        return (s.Extension == ".exe") || (s.Extension == ".dll");
                    })
                    .OrderBy((s2) =>
                    {
                        return s2.Name;
                    })
                    ;

                foreach (var file in files)
                {
                    Console.WriteLine(string.Format("=== File {0} ====", file.FullName));
                    DumpFileReferences(file.FullName);
                }
            }
        }

        Console.ReadKey(false);
    }

    private static void DumpFileReferences(string fileName)
    {
        try
        {
            var assembly = Assembly.ReflectionOnlyLoadFrom(fileName);
            var refs = assembly.GetReferencedAssemblies();

            foreach (var x in refs.OrderBy((s) =>
            {
                return s.Name;
            }))
            {
                PrintFilename(x);
            }
        }
        catch (BadImageFormatException bif)
        {
            Console.WriteLine(bif.Message);
        }
        catch (FileLoadException fl)
        {
            Console.WriteLine(fl.Message);
        }
    }

    private static void PrintFilename(AssemblyName x)
    {
        if (x.Name.StartsWith("Apdcomms"))
        {
            Console.ForegroundColor = ConsoleColor.Magenta;
        }
        Console.WriteLine(string.Format("{1,-10} {0}", x.Name, x.Version));
        Console.ResetColor();
    }
}
}

解决方案

It is not that difficult to create such a tool yourself:

var assembly = Assembly.ReflectionOnlyLoadFrom(@"..the file...");

var refs = assembly.GetReferencedAssemblies();

refs will then contain the direct references.