Mono.Cecil something like Type.GetInterfaceMap?

System.Reflection.Type contains GetInterfaceMap , which help determine which method implements a method from the interface.

Does Mono.Cecil have something like this? Or how to implement this behavior?

+6
c # mono.cecil
source share
1 answer

No, Cecil does not provide such a method, because Cecil gives us only the CIL metadata as it is. (There is a Cecil.Rocks project that contains some useful extension methods, but not this one)

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

The following code snippets can help you with 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 helper method GetOriginalBaseMethod() return method.GetOriginalBaseMethod().IsEqual(overridden); } /// <summary> /// Implicit interface implementations are based only on method 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... 
+7
source share

All Articles