views:

69

answers:

1

I am trying to find all the types that a given type is dependent on, including interfaces, abstract classes, enums, structs, etc. I want to load up an assembly, and print out a list of all the types defined within it, and their dependencies.

So far I have been able to find all the the external types a CLR assembly depends on using Mono.Cecil, e.g.

using System;
using Mono.Cecil;
using System.IO;

FileInfo f = new FileInfo("SomeAssembly.dll");
AssemblyDefinition assemDef = AssemblyFactory.GetAssembly (f.FullName); 
List<TypeReference> trList = new List<TypeReference>();

foreach(TypeReference tr in assemblyDef.MainModule.TypeReferences){
    trList.Add(tr.FullName);
}

This list can also be obtained using the mono disasembler, eg "monodis SomeAssembly.dll --typeref", but this list doesnt seem to include primitives, eg System.Void, System.Int32, etc

I need to treat each type individually, and get all types that a given type depends on, even if the types are defined in the same assembly. Is there any way to do this using Mono.Cecil, or any other project?

I know it can be done by loading the assembly, then iterating over each defined type, then loading the IL of the type and scanning it for references, but I am sure that there is a better way. Ideally it will also work with anonymous inner classes.

It should also work if multiple modules are defined in the same assembly.

+1  A: 

Have a look at NDepend - it does this, and much more.

-Oisin

x0n
Thanks Oisin,I know about NDepend, it is a great product. I am trying to generate a list of dependent types so I can feed it into another tool. Hence NDepend is not the tool I need.
aj.esler