【问题标题】:How do I find all the type dependecies of a given type in any CLR based language assembly?如何在任何基于颜色的语言程序集中找到给定类型的所有类型依赖项?
【发布时间】:2011-01-04 22:36:07
【问题描述】:

我正在尝试查找给定类型所依赖的所有类型,包括接口、抽象类、枚举、结构等。我想加载一个程序集,并打印出其中定义的所有类型的列表它,以及它们的依赖关系。

到目前为止,我已经能够使用 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 并扫描它以查找引用来完成,但我确信有更好的方法。理想情况下,它也适用于匿名内部类。

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

【问题讨论】:

    标签: c# clr cil mono.cecil


    【解决方案1】:

    阿杰, 我遇到了同样的问题,我需要遍历程序集中的类型,然后我决定使用 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);
                }
            }
    
          }
    

    【讨论】:

      【解决方案2】:

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

      -奥辛

      【讨论】:

      • 感谢 Oisin,我知道 NDepend,它是一个很棒的产品。我正在尝试生成依赖类型的列表,以便可以将其提供给另一个工具。因此 NDepend 不是我需要的工具。
      猜你喜欢
      • 1970-01-01
      • 2021-05-25
      • 1970-01-01
      • 2010-11-27
      • 1970-01-01
      • 1970-01-01
      • 2018-12-27
      • 2015-03-29
      • 1970-01-01
      相关资源
      最近更新 更多