【问题标题】:Finding type hierarchy assemblies using Mono.Cecil使用 Mono.Cecil 查找类型层次结构程序集
【发布时间】:2011-06-13 18:02:03
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: c# reflection assemblies mono.cecil


    【解决方案1】:

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

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

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

    【讨论】:

    • 谢谢!这很有帮助:)
    • 塞西尔的作者也一样!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-05
    • 1970-01-01
    • 2022-10-18
    • 2021-09-29
    • 2013-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多