Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在任何基于CLR的语言程序集中找到给定类型的所有类型依赖项?_C#_Clr_Cil_Mono.cecil - Fatal编程技术网

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

C# 如何在任何基于CLR的语言程序集中找到给定类型的所有类型依赖项?,c#,clr,cil,mono.cecil,C#,Clr,Cil,Mono.cecil,我试图找到给定类型所依赖的所有类型,包括接口、抽象类、枚举、结构等。我想加载一个程序集,并打印出其中定义的所有类型及其依赖关系的列表 到目前为止,我已经能够找到CLR程序集使用Mono.Cecil所依赖的所有外部类型,例如 using System; using Mono.Cecil; using System.IO; FileInfo f = new FileInfo("SomeAssembly.dll"); AssemblyDefinition assemDef = AssemblyFac

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

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

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);
}
使用系统;
使用单盲法;
使用System.IO;
FileInfo f=newfileinfo(“SomeAssembly.dll”);
AssemblyDefinition assemDef=AssemblyFactory.GetAssembly(f.FullName);
List trList=新列表();
foreach(assemblyDef.MainModule.TypeReferences中的类型引用tr){
trList.Add(tr.FullName);
}
该列表也可以使用mono分解器获得,例如“monodis SomeAssembly.dll--typeref”,但该列表似乎不包括原语,例如System.Void、System.Int32等

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

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


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

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

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

      }

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