2013-10-01 7 views
5

हाय में MethodInfo से एक प्रतिनिधि बनाना। मैंने सोचा कि जब वे "सब्सक्राइब" विशेषता के साथ सजाए जाते हैं तो मैसेंजर को स्वत: सब्सक्राइब विधियों के लिए एक सहायक बनाने के लिए निफ्टी होगा।मैं मोनो 2.8.2 में एक दूत बनाने के लिए कोशिश कर रहा हूँ मोनो 2.8.2

मैं इस पर मेरे सिर खरोंच किया गया है और मेरी समस्या का समाधान के बिना अन्य संबंधित ढेर सवालों के कई पढ़ा है। Frankly, I don't know if I am doing something wrong or if this is a bug in Mono.

foreach (var methodInfo in methods) 
     { 
      var attr = methodInfo.GetAttribute<SubscribeAttribute>(); 
      if (attr == null) 
       continue; 

      var parmas = methodInfo.GetParameters(); 
      if (parmas.Length != 1) 
      { 
       Debug.LogError("Subscription aborted. Invalid paramters."); 
       continue; 
      } 

      var type = parmas[0].ParameterType; 

      // Crashes here 
      // ArgumentException: method argument length mismatch 
      // I have tried many combinations.. 
      // Direct typing of the message type and dynamic typing 

      var action = (Action<object>)Delegate.CreateDelegate(typeof(Action<object>), methodInfo); 

      // also does not work 
      // var dt = Expression.GetActionType(parmas.Select(o => o.ParameterType).ToArray()); 
      // var action = Delegate.CreateDelegate(dt, methodInfo); 

      Subscribe(type, action, instance); 
     } 

किसी भी सुझाव या कार्य के आसपास सराहना की जाएगी।

संपादित विधि हस्ताक्षर लगता है:

[Subscribe] 
void OnMessage(object message){ 
    // Hello World 
} 

हालांकि, यह मूल रूप से था ...

[Subscribe] 
void OnTestMessage(TestMessage message){ 
    // Hello World 
} 
+0

विधि आप की सदस्यता के लिए कोशिश कर रहे हैं के हस्ताक्षर क्या है? क्या इसमें 'शून्य MyMethod (ऑब्जेक्ट Arg)' जैसे हस्ताक्षर हैं? –

+0

सही। मैंने पोस्ट अपडेट किया है। नहीं एक विकल्प: – user2085865

+1

मोनो 2.8 बेहद पुराना है, 3.2.3 – knocte

उत्तर

6

यह एक गैर स्थैतिक विधि है और आप एक लक्ष्य नहीं प्रदान की थी वस्तु। इसलिए Delegate.CreateDelegate एक स्पष्ट this तर्क के साथ एक "खुला प्रतिनिधि" पैदा करेगा।

आवश्यक this तर्क की वजह से

, यह अब हस्ताक्षर से मेल खाता है।

+0

मैं खुद को किसी मुद्दे पर घायल कर देता हूं और स्पष्ट याद करता हूं। मुझे कॉफी छोड़ना और मेरी बिल्ली के साथ खेलना सीखना है। – user2085865

+1

यह एक लंबा समय रहा है लेकिन यह जवाब वास्तव में मदद करता है। '(एक्शन ) प्रतिनिधि। क्रिएटडिलेगेट (टाइपोफ (एक्शन ), ** यह **, methodInfo); 'इसे सही बना देगा। –