Mono.Cecil能做到像Type.GetInterfaceMap? [英] Mono.Cecil something like Type.GetInterfaceMap?

查看:164
本文介绍了Mono.Cecil能做到像Type.GetInterfaceMap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.Reflection.Type 包含 GetInterfaceMap 这有助于确定哪些方法实现从接口的一些方法。

System.Reflection.Type contains GetInterfaceMap which help to determine what method implement some method from interface.

确实 Mono.Cecil能做到包含这样的事情?
或如何实现这样的行为?

Does Mono.Cecil contain something like this? Or how to implement such behaviour?

推荐答案

没有,丝丝不提供这样的方法,因为塞西尔给我们只是CIL的元数据,因为它们。 (有项目Cecil.Rocks,其中包含了一些有用的扩展方法,但不是这个)

No, Cecil does not provide such method, because Cecil gives us just CIL metadata as they are. (There is project Cecil.Rocks, which contains some useful extension methods, but not this one)

在MSIL方法有一个属性覆盖,其中包含的方法的引用该方法覆盖(在塞西尔的确有在MethodDefinition类的属性覆盖)。然而,该属性用于仅在某些特殊情况下,如显式接口实现。通常这个属性为空的,哪些方法是由有问题的方法重写是基于约定。这些约定在ECMA CIL的标准进行了说明。总之方法将覆盖具有相同的名称和相同的签名方法

In MSIL methods have an attribute 'overrides', which contains references to methods that this method overrides (in Cecil there is indeed a property Overrides in the MethodDefinition class). However, this attribute is used only in some special cases such as explicit interface implementation. Usually this attribute is left empty and which methods are overridden by the method in question is based on conventions. These conventions are described in ECMA CIL standard. In short a method overrides methods that have the same name and the same signature.

下面的代码段可能会帮助你和这个讨论的http://groups.google.com/group/mono-cecil/browse_thread/thread/b3c04f25c2b5bb4f/c9577543ae8bc40a

The following pieces of code might help you as well as this discussion: http://groups.google.com/group/mono-cecil/browse_thread/thread/b3c04f25c2b5bb4f/c9577543ae8bc40a

    public static bool Overrides(this MethodDefinition method, MethodReference overridden)
    {
        Contract.Requires(method != null);
        Contract.Requires(overridden != null);

        bool explicitIfaceImplementation = method.Overrides.Any(overrides => overrides.IsEqual(overridden));
        if (explicitIfaceImplementation)
        {
            return true;
        }

        if (IsImplicitInterfaceImplementation(method, overridden))
        {
            return true;
        }

        // new slot method cannot override any base classes' method by convention:
        if (method.IsNewSlot)
        {
            return false;
        }

        // check base-type overrides using Cecil's helper method GetOriginalBaseMethod()
        return method.GetOriginalBaseMethod().IsEqual(overridden);
    }

    /// <summary>
    /// Implicit interface implementations are based only on method's name and signature equivalence.
    /// </summary>
    private static bool IsImplicitInterfaceImplementation(MethodDefinition method, MethodReference overridden)
    {
        // check that the 'overridden' method is iface method and the iface is implemented by method.DeclaringType
        if (overridden.DeclaringType.SafeResolve().IsInterface == false ||
            method.DeclaringType.Interfaces.None(i => i.IsEqual(overridden.DeclaringType)))
        {
            return false;
        }

        // check whether the type contains some other explicit implementation of the method
        if (method.DeclaringType.Methods.SelectMany(m => m.Overrides).Any(m => m.IsEqual(overridden)))
        {
            // explicit implementation -> no implicit implementation possible
            return false;
        }

        // now it is enough to just match the signatures and names:
        return method.Name == overridden.Name && method.SignatureMatches(overridden);
    }

    static bool IsEqual(this MethodReference method1, MethodReference method2)
    {
        return method1.Name == method2.Name &&  method1.DeclaringType.IsEqual(method2.DeclaringType);
    }
    // IsEqual for TypeReference is similar...

这篇关于Mono.Cecil能做到像Type.GetInterfaceMap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆