【问题标题】:Use Mono.Cecil to determine if a type implements an interface使用 Mono.Cecil 确定类型是否实现接口
【发布时间】:2012-06-23 23:52:01
【问题描述】:

我浏览了 Wiki 上的文档,但似乎有点薄。如何确定一个类型是否使用 Cecil 实现了给定的接口?对于我的特定实现,重要的是我实际上并未将类型加载到 AppDomain 中。

这是我目前的代码:

Dim outputModule As ModuleDefinition = ModuleDefinition.ReadModule(outputFile)

For Each assemblyType As TypeDefinition In outputModule.Types
    'How to determine if assemblyType implements a specific interface?
Next

【问题讨论】:

    标签: .net vb.net mono.cecil


    【解决方案1】:

    在 C# 中我已经实现了它比较接口的全名:

    if (assemblyType.Interfaces.Any(
      type => type.Resolve().FullName == typeof(YourInterface).FullName
    )) {
      // ...
    }
    

    如果您有适合您的界面的TypeDefinitions,您也可以比较。

    【讨论】:

    • 谢谢,你能解释一下 Resolve 方法在做什么,或者给我一些文档吗?它没有将类型加载到 AppDomain 中是吗?
    • @Matt: ResolveTypeReference 获取TypeDefinition。基本上,一个类型可以在很多地方被引用,您可以使用Resolve 从其中一个引用中获取其定义。
    • ...不,它没有在应用程序域中加载类型。
    • 听起来正是我需要的。谢谢!
    • 不会 type.FullName 代替 type.Resolve().FullName 工作吗?它也会更快
    猜你喜欢
    • 1970-01-01
    • 2010-10-04
    • 1970-01-01
    • 2011-08-23
    • 2013-02-14
    • 2022-01-22
    • 2010-09-13
    • 1970-01-01
    相关资源
    最近更新 更多