Search Unity

Delegate.CreateDelegate cant connect with correct overloaded function..

Discussion in 'Scripting' started by BurnedZombie, Jun 7, 2016.

  1. BurnedZombie

    BurnedZombie

    Joined:
    Jun 7, 2016
    Posts:
    3
    i want connect a custom event with two different classes.
    Code (csharp):
    1.  
    2. public class MethodHaveClass()
    3. {
    4.      public static void ClickEventTest()
    5.      {   Debug.Log("Clicked!!");
    6. }
    7.  
    with
    Code (csharp):
    1.  
    2. public class EventHaveClass()
    3. {
    4.      public delegate void CustomEvent();
    5.      public event CustomEvent OnClicked;
    6. }
    7.  
    and try this..
    Code (csharp):
    1.  
    2. public class DoSthClass : EventHaveClass()
    3. {
    4.    public void Init()
    5.   {
    6.     Type methodClassType = Type.GetType("MethohHaveClass");
    7.     MethodInfo m_Info = methodClassType.GetMethod("ClickEventTest");
    8.  
    9.     Type eventClassType = Type.GetType("EventHaveClass");
    10.     EventInfo m_event = eventClassType.GetEvent("OnClicked");
    11.  
    12.     Delegate handler = Delegate.CreateDelegate(m_event.EventHandlerType, m_Info); // <- exception
    13.     m_event.AddEventHandler(eventClassType, handler);
    14.   }
    15. }
    16.  
    then unity console says.
    ArgumentException: method argument length mismatch
    System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, Boolean throwOnBindFailure) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Delegate.cs:226)
    ....
    whats wrong with this code?
    i just try to use CreateDelegate(Type, MethodInfo) this type of overload method but unity try to connect
    CreateDelegate(Type, Object, MethodInfo, Boolean) this type every time.. even if i try to use CreateDelegate(Type, Object, MethodInfo, Boolean) type , unity shows same ArgumentException again..

    anything wrong with this??
    i tried over 3days but still no idea.
    i hope your kindness.
     
    Last edited: Jun 7, 2016
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
  3. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    Do you really need to do this through reflection?
     
  4. BurnedZombie

    BurnedZombie

    Joined:
    Jun 7, 2016
    Posts:
    3
  5. BurnedZombie

    BurnedZombie

    Joined:
    Jun 7, 2016
    Posts:
    3
    cause in my real code..
    MethodHaveClass is create in runtime. so i cant confirm that class name before it was created.
    so more specific Init() code like..
    Code (csharp):
    1.  
    2. public class DoSthClass : EventHaveClass()
    3. {
    4. public void Init(string methodClassName, string methodName, string eventClassName string eventName)
    5. {
    6. Type methodClassType = Type.GetType(methodClassName);
    7. MethodInfo m_Info = methodClassType.GetMethod(methodName);
    8.  
    9. Type eventClassType = Type.GetType(eventClassName);
    10. EventInfo m_event = eventClassType.GetEvent(eventName);
    11.  
    12. Delegate handler = Delegate.CreateDelegate(m_event.EventHandlerType, m_Info); // <- exception
    13. m_event.AddEventHandler(eventClassType, handler);
    14. }
    15. }
    16.  
    im try to make (C# - WinForm like) Style Editor UI System.
    so new EditorWindow("MethodHaveClass") is going to make. then Controls ("EventHaveClass") added on EditorWindow.
    in this case. i think i need this reflection code.. to use added controls..
     
    Last edited: Jun 7, 2016
  6. ThibaultGouala

    ThibaultGouala

    Joined:
    May 30, 2016
    Posts:
    12
    You are creating a delegate without specifying a target. This would work for a static method. In newer version of .Net framework CreateDelegate supports instance method without specifying any target, but it is very possible that the mono version used by Unity does not support that.