Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find all the type dependecies of a given type in any CLR based language assembly?

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.

like image 803
aj.esler Avatar asked Jan 14 '10 03:01

aj.esler


1 Answers

AJ, I had the same issue where I needed to traverse the Types in an assembly and I settled on using Mono.Cecil. The way I was able to walk through each Class and if a Property in a Class was not another Class instead of a CLR type was through a recursive function.

    private void BuildTree(ModuleDefinition tempModuleDef , TypeDefinition tempTypeDef, TreeNode rootNode = null)
    {
            AssemblyTypeList.Add(tempTypeDef);

            TreeNode tvTop = new TreeNode(tempTypeDef.Name);

            // list all properties
            foreach (PropertyDefinition tempPropertyDef in tempTypeDef.Properties)
            {
                //Check if the Property Type is actually a POCO in the same Assembly
                if (tempModuleDef.Types.Any(q => q.FullName == tempPropertyDef.PropertyType.FullName))
                {
                    TypeDefinition theType = tempModuleDef.Types.Where( q => q.FullName == tempPropertyDef.PropertyType.FullName)
                                                                .FirstOrDefault();
                    //Recursive Call
                    BuildTree(tempModuleDef, theType, tvTop);

                }

                TreeNode tvProperty = new TreeNode(tempPropertyDef.Name);
                tvTop.Nodes.Add(tvProperty);
            }

            if (rootNode == null)
                tvObjects.Nodes.Add(tvTop);
            else
                rootNode.Nodes.Add(tvTop);

    }

This Function is called by my main Function the gist of which is

      public void Main()
      {
        AssemblyDefinition  assemblyDef = AssemblyDefinition.ReadAssembly(dllname);

        //Populate Tree
        foreach (ModuleDefinition tempModuleDef in assemblyDef.Modules)
        {
            foreach (TypeDefinition tempTypeDef in tempModuleDef.Types)
            {
                BuildTree(tempModuleDef ,tempTypeDef, null);
            }
        }

      }
like image 160
Vishnoo Rath Avatar answered Sep 19 '22 14:09

Vishnoo Rath