Mark A. Richman said:
Does anyone know of a tool, similar to Depends.exe (Dependency Walker) for
.NET assemblies? I'm looking for a way to report on inter-assembly
dependencies. Maybe a Visio plugin that would help map out dependencies
without littering the UI with properties, methods, etc?
Here's a fairly simple thing: give it the name of which
assembly/assemblies you want it to look through. If an entry appears in
square brackets, that means the dependencies for that assembly have
been listed earlier. Let me know if you have any problems with it.
using System;
using System.Reflection;
using System.Collections;
public class DependencyReporter
{
static void Main(string[] args)
{
try
{
if (args.Length==0)
{
Console.WriteLine
("Usage: DependencyReporter <assembly1> [assembly2 ...]");
}
Hashtable alreadyLoaded = new Hashtable();
foreach (string name in args)
{
Assembly assm = Assembly.LoadFrom (name);
DumpAssembly (assm, alreadyLoaded, 0);
}
}
catch (Exception e)
{
Console.WriteLine ("Error: {0}", e.Message);
}
}
static void DumpAssembly (Assembly assm, Hashtable alreadyLoaded,
int indent)
{
Console.Write (new String(' ', indent));
AssemblyName fqn = assm.GetName();
if (alreadyLoaded.Contains(fqn.FullName))
{
Console.WriteLine ("[{0}]", fqn.Name);
return;
}
alreadyLoaded[fqn.FullName]=fqn.FullName;
Console.WriteLine (fqn.Name);
foreach (AssemblyName name in assm.GetReferencedAssemblies())
{
Assembly referenced = Assembly.Load(name);
DumpAssembly(referenced, alreadyLoaded, indent+2);
}
}
}