6

我正在尝试实现一个接收类型并返回包含其基类型的所有程序集的方法。

例如:
A是基类型(类A属于程序集c:\A.dll
B继承自A(类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" }
}

我的主要问题是AssemblyDefinitionMono.Cecil 不包含任何像Location这样的属性。

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

4

1 回答 1

5

一个程序集可以由多个模块组成,因此它本身并没有真正的位置。程序集的主模块确实有一个位置:

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

或任何其他更让您满意的变体。

于 2011-01-22T17:55:09.897 回答