2011-02-07 2 views

답변

7

아니요, Cecil은 CIL 메타 데이터를 그대로 제공하므로 이러한 방법을 제공하지 않습니다. (유용한 Cemil.Rocks 프로젝트가 있지만 유용한 확장 메서드가 포함되어 있습니다.)

MSIL 메서드에는이 메서드가 재정의하는 메서드에 대한 참조를 포함하는 '재정의'특성이 있습니다 (Cecil에는 실제로 property MethodDefinition 클래스에서 재정 의됨). 그러나이 속성은 명시 적 인터페이스 구현과 같은 일부 특수한 경우에만 사용됩니다. 일반적으로이 애트리뷰트는 비어 있으며 문제의 메소드에 의해 대체되는 메소드는 규칙을 기반으로합니다. 이러한 규칙은 ECMA CIL 표준에 설명되어 있습니다. 간단히 말해서 메서드는 동일한 이름과 동일한 서명을 가진 메서드를 재정의합니다. 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... 
+0

당신이 누락 된'SignatureMatches' 방법을 제공하시기 바랍니다 수 : 코드의

다음 조각이 토론뿐만 아니라 당신을 도울 수 있는가? 이것은 쉽게 비교할 수없는 일반 매개 변수와 인수를 고려해야하기 때문에 복잡한 것입니다. – ygoe