简体   繁体   English

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

[英]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, eg 到目前为止,我已经能够找到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);
}

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 此列表也可以使用mono disasembler获得,例如“monodis SomeAssembly.dll --typeref”,但此列表似乎不包括基元,例如System.Void,System.Int32等

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? 有没有办法使用Mono.Cecil或任何其他项目?

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. 我知道可以通过加载程序集,然后迭代每个定义的类型,然后加载类型的IL并扫描它以获取引用来完成,但我确信有更好的方法。 Ideally it will also work with anonymous inner classes. 理想情况下,它也适用于匿名内部类。

It should also work if multiple modules are defined in the same assembly. 如果在同一个程序集中定义了多个模块,它也应该有效。

AJ, I had the same issue where I needed to traverse the Types in an assembly and I settled on using Mono.Cecil. AJ,我有同样的问题,我需要遍历程序集中的类型,我决定使用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. 我能够遍历每个类并且如果类中的属性不是另一个类而不是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);

    }

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

      }

Have a look at NDepend - it does this, and much more. 看看NDepend - 它可以做到这一点,还有更多。

-Oisin -Oisin

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从XAML中CLR名称空间的类型而不是程序集(而不是声明类型)中映射类型? - How do I map a type from a CLR namespace in XAML from an assembly other than the one where it's declared? 如何在抽象类中声明任何类型的数组(从给定类型派生)? - How do I declare an array of any type (derived from a given type) in an abstract class? 如何找到OData edm模型类型到clr类型的映射? - How to find mapping of OData edm model type to clr type? Npgsql 6.x:如何查找 CLR 类型的类型映射 - Npgsql 6.x: How to find type mapping for CLR Type 给定一个 ANTLR ParserRuleContext,我如何找到给定类型的第一个父级? - Given an ANTLR ParserRuleContext, how do I find the first parent of a given type? 如何在某种类型的数组中找到所有元素? - How do I find all elements in an array of a certain type? 如何在类中找到DateTime类型的所有属性? - How do I find all properties of type DateTime in an class? Neo4JClient:如何找到给定节点的关系列表(任何类型)? - Neo4JClient: How can I find a list of relationships (of any type) for a given node? AssemblyLoadContext找不到基于类型的程序集 - AssemblyLoadContext cannot find in assembly based on type 对于任何值类型T,如何确定对象的类型是否为IEnumerable <T>的子类? - How do I find out whether an object's type is a subclass of IEnumerable<T> for any value type T?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM