2011-01-22 38 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