如何在任何基于CLR的语言程序集中找到给定类型的所有类型依赖关系?

6

我希望能够找到一个给定类型所依赖的所有类型,包括接口、抽象类、枚举和结构体等。我想加载一个程序集,并打印出其中定义的所有类型及其依赖关系。

到目前为止,使用Mono.Cecil,我已经能够找到CLR程序集依赖的所有外部类型,例如:

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);
}

这个列表也可以使用mono反汇编器获得,例如"monodis SomeAssembly.dll --typeref",但是这个列表似乎不包括基元类型,例如System.Void,System.Int32等。

我需要单独处理每种类型,并获取给定类型所依赖的所有类型,即使这些类型在同一个程序集中定义。是否有任何使用Mono.Cecil或其他项目来实现此目的的方法?

我知道可以通过加载程序集,然后迭代每个定义的类型,然后加载该类型的IL并扫描其引用来完成此操作,但我相信有更好的方法。理想情况下,它还将适用于匿名内部类。

如果在同一个程序集中定义了多个模块,它也应该起作用。

2个回答

1
AJ, 我曾经遇到过同样的问题,需要遍历程序集中的类型,最终我选择使用Mono.Cecil。我通过递归函数来遍历每个类,如果一个类中的属性不是CLR类型而是另一个类,就可以找到它了。
    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);

    }

这个函数是由我的主函数调用的,其要点是:

      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);
            }
        }

      }

-1

看看NDepend - 它可以做到这一点,还有更多。

-Oisin


谢谢Oisin, 我知道NDepend,它是一个很棒的产品。 我正在尝试生成一个依赖类型的列表,以便我可以将它输入到另一个工具中。因此,NDepend并不是我需要的工具。 - aj.esler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接