2011-02-07 11 views
6

सिस्टम। रिफ्लेक्शन। टाइप में GetInterfaceMap है जो यह निर्धारित करने में सहायता करता है कि कौन सी विधि इंटरफ़ेस से कुछ विधि लागू करती है।मोनो। कुछ टाइप करें जैसे Type.GetInterfaceMap?

Mono.Cecil इस तरह कुछ शामिल है? या ऐसे व्यवहार को कैसे कार्यान्वित करें?

उत्तर

7

नहीं, सेसिल इस तरह की विधि प्रदान नहीं करता है, क्योंकि सेसिल हमें केवल सीआईएल मेटाडेटा देता है। (परियोजना Cecil.Rocks है, जिसमें कुछ उपयोगी विस्तार विधियां हैं, लेकिन यह नहीं)

एमएसआईएल विधियों में एक विशेषता 'ओवरराइड' है, जिसमें इस विधि को ओवरराइड करने के तरीकों के संदर्भ शामिल हैं (सेसिल में वास्तव में एक है विधि विधि परिभाषा वर्ग में ओवरराइड)। हालांकि, यह विशेषता केवल कुछ विशेष मामलों जैसे स्पष्ट इंटरफ़ेस कार्यान्वयन में उपयोग की जाती है। आम तौर पर यह विशेषता खाली छोड़ दी जाती है और प्रश्नों के तरीके से कौन सी विधियों को ओवरराइड किया जाता है, यह सम्मेलनों पर आधारित होता है। ईसीएमए सीआईएल मानक में इन सम्मेलनों का वर्णन किया गया है। संक्षेप में एक विधि उन विधियों को ओवरराइड करती है जिनमें समान नाम और समान हस्ताक्षर होते हैं। 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