11

I begun work in a new project with lots of assemblies in a single solution. I am not familiar yet with the assembly dependencies and having a hard time figuring out which assembly depends on another.

Do you know any tools that are capable to show a dependency list or better a visual graph of it?

Any help appreciated !

Jens Björnhager
  • 5,632
  • 3
  • 27
  • 47
SvenG
  • 5,155
  • 2
  • 27
  • 36

2 Answers2

17

Here is some quick code to show case the Cecil Library to do this:

 

public static void PoC(IEnumerable<AssemblyDefinition> assemblies, TextWriter writer)
{
    Console.WriteLine("digraph Dependencies {");
    var loaded = assemblies
        .SelectMany(a => a.Modules.Cast<ModuleDefinition>())
        .SelectMany(m => m.AssemblyReferences.Cast<AssemblyNameReference>().Select(a => a.Name + ".dll"))
        .Distinct()
        .Select(dllname => {
               try { return AssemblyFactory.GetAssembly(dllname); }
               catch { return null; } })
        .Where(assembly => assembly != null)
        .ToList();

    loaded.ForEach(a => a.MainModule.FullLoad());

    loaded.ForEach(a =>
        {
            foreach (var r in a.MainModule.AssemblyReferences.Cast<AssemblyNameReference>())
                Console.WriteLine(@"""{0}"" -> ""{1}"";", r.Name, a.Name.Name);
        } );

    Console.WriteLine("}");
}

It generates a dot graph file. Running this on a fairly simple project results in:

enter image description here

Running it on a slightly less simple project returned this:

enter image description here

It may be advisable to filter out certain assemblies (.StartsWith("System.")?) and / or limit search depth etc.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • awesome! (You should update the code to Cecil .9 to save you all the Cast<>()). – Jb Evain Feb 13 '12 at 15:43
  • +1 for your image after "Running it on a slightly less simple project returned this". – Steven Feb 13 '12 at 15:44
  • @JbEvain: I'm behind on updates. Cecil serves me well many times but I don't get nearly enough time to write my tools. Thanks for the kind words (@others: JbE is the author of Cecil, the awesome library making this a breeze) – sehe Feb 13 '12 at 15:45
  • 1
    That looks cool, but as we already have NDepend in use in our company its the quicker and more effecient way for me, but thanks for your detailed explanations on this ! – SvenG Feb 13 '12 at 16:28
  • @SvenG: Huh/Stupified. You already have NDepend? I'm completely at a loss why the question got asked now. – sehe Feb 13 '12 at 16:30
  • We have bought it recently but I haven't been using it so far as I am not familiar with it and haven't had the time to investigate all its functionalities. We will receive a training on it in the future .. but have too many busy projects running atm. – SvenG Feb 13 '12 at 16:58
  • @SvenG: well, fair enough, I suppose. I'd have thought that the name `NDepend` would be a kind of a giveaway :) – sehe Feb 13 '12 at 17:04
7

NDepend is the king when it comes to dependency graph analysis. NDepend is a commercial tool that proposes:

See all details in this Stackoverflow answer concerning a related question.

Steven
  • 166,672
  • 24
  • 332
  • 435
  • 1
    Thanks for all suggestions. We already have NDepend in use for Code Metrix/code quality checks, but I haven't realized until now that it can create a code dependency graph also. Have tested it and works like a charm. Its exactly what I needed !!! – SvenG Feb 13 '12 at 16:27