[英]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.