Finding type hierarchy groups using Mono.Cecil

I am trying to implement a method that receives a type and returns all assemblies that contain its base types.

For example:
The class Ais the base type (the class Abelongs to the assembly c: \ A.dll)
The class Binherits from A(the class Bbelongs to the assembly c: \ B.dll)
The class Cinherits from B(the class Cbelongs to the assembly c: \ c.dll)

public IEnumerable<string> GetAssembliesFromInheritance(string assembly, 
                                                        string type)
{
    // If the method recieves type C from assembly c:\C.dll
    // it should return { "c:\A.dll", "c:\B.dll", "c:\C.dll" }
}

My main problem is that AssemblyDefinitionof Mono.Cecil does not contain any property like Location.

How to find assembly location with AssemblyDefinition?

+5
source share
1 answer

An assembly may consist of several modules, so in reality it does not have a place as such. The main build module has a location, though:

AssemblyDefinition assembly = ...;
ModuleDefinition module = assembly.MainModule;
string fileName = module.FullyQualifiedName;

So you can write something along the line:

public IEnumerable<string> GetAssembliesFromInheritance (TypeDefinition type)
{
    while (type != null) {
        yield return type.Module.FullyQualifiedName;

        if (type.BaseType == null)
            yield break;

        type = type.BaseType.Resolve ();
    }
}

Or any other option that you like more.

+3
source

All Articles