2011-02-07 3 views

答えて

7

いいえ、CecilはCILメタデータをそのまま提供するため、このような方法は提供していません。

MSILのメソッドには、このメソッドがオーバーライドするメソッドへの参照を含む「オーバーライド」属性があります(Cecilには実際には存在しません。 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

関連する問題