Hi,
For each type in an assembly, I want to get a list of all the types
that the given type is dependent on, i.e. those that are used as
fields, properties, in methods, and anywhere else the type could use
them. I want to include any types that happen to be defined in the
same assembly.
Basically, the idea is to load an assembly, and for each type defined
in the assembly, list all the type dependencies.
So far I have been able to get a list of used types from the assembly,
and some dependencies, with
FileInfo f = new FileInfo("SomeAssembly.dll");
AssemblyDefinition assemDef = AssemblyFactory.GetAssembly
(f.FullName);
foreach(ModuleDefinition mDef in assemDef.Modules){
foreach (TypeDefinition type in mDef.Types) {
if(!"<Module>".Equals(type.FullName)){
Console.WriteLine (type.Name+" ["+type.Namespace+"]");
foreach(MethodDefinition method in type.Methods)
{
if(method.HasBody){
foreach(Instruction instr in
method.Body.Instructions){
if(instr.OpCode.OperandType ==
OperandType.InlineType){
Console.WriteLine
("\tUses type "+instr.Operand);
}
}
}
}
}
}
}
but this doesn't work very well, as it doesn't list types defined in
the assembly. It also misses a lot of types used in the code.
The plan is to have this dependency generator the basis of a
dependency analyser. Are there any tools that can do this and the
source code is available? I know of some such as reflector and
NDepend, but I cannot see how they generate the list, as they do not
seem to be open source.
I will have a look at Gendarme next, and see if that can help, but it
seems focused in a different area.
Is there any way of getting this information using Cecil, or an other
libraries, or am I going to have to go through the IL line by line
looking for opcodes?
--
--
mono-cecil