2011-01-22 122 views
5

我想实现一个方法,它接收一个类型并返回包含其基类型的所有程序集。使用Mono.Cecil查找类型层次结构程序集

例如:
A是一个基本类型(类A属于组件C:\ A.DLL
BA(类继承B属于程序集c:\ B.dll
类别C继承B(类C属于装配C:\ c.dll

public IEnumerable<string> GetAssembliesFromInheritance(string assembly, 
                 string type) 
{ 
    // If the method recieves type C from assembly c:\C.dll 
    // it should return { "c:\A.dll", "c:\B.dll", "c:\C.dll" } 
} 

我的主要问题是,AssemblyDefinition从Mono.Cecil能不包含像位置的任何财产

如何在给定AssemblyDefinition的情况下找到组装位置?

回答

3

一个组件可以由多个模块组成,所以它本身并没有真正的位置。大会的主要模块确实有虽然位置:

AssemblyDefinition assembly = ...; 
ModuleDefinition module = assembly.MainModule; 
string fileName = module.FullyQualifiedName; 

所以,你可以写沿着线的东西:

public IEnumerable<string> GetAssembliesFromInheritance (TypeDefinition type) 
{ 
    while (type != null) { 
     yield return type.Module.FullyQualifiedName; 

     if (type.BaseType == null) 
      yield break; 

     type = type.BaseType.Resolve(); 
    } 
} 

或取悦你更多的其他变种。

+0

谢谢!这是非常有用的:) – Elisha 2011-01-22 18:06:02