- Home /
Create delegate from MethodInfo
The problem I'm facing is that I'm getting an error when creating said delegate.
It's telling me that the arguments are incompatible.
ArgumentException: method arguments are incompatible
System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, System.Boolean throwOnBindFailure, System.Boolean allowClosed) (at <9577ac7a62ef43179789031239ba8798>:0)
System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method, System.Boolean throwOnBindFailure) (at <9577ac7a62ef43179789031239ba8798>:0)
System.Delegate.CreateDelegate (System.Type type, System.Reflection.MethodInfo method) (at <9577ac7a62ef43179789031239ba8798>:0)
EventCatcher.Start () (at Assets/EventCatcher.cs:48)
What I don't understand is that the arguments seem to be aligned. The following is the declaration of the function I want to call, the declaration of the delegate and the creation of the delegate.
public void __Attack_SetFocused() // this is the function I want to call. (In file A)
public delegate void OnClickFunction(); // this is the declaration of the delegate (In file B)
OnClickFunction onClickFunction; // this is the variable I create (in file B)
The assignment of the delegate is below:
System.Type inf = typeof(ButtonBehaviors);
foreach (var method in inf.GetMethods())
{
if (method.Name == "__Attack_SetFocused")
{
onClickFunction = (OnClickFunction)System.Delegate.CreateDelegate(typeof(OnClickFunction), method);
}
}
Clearly I'm not interpreting the error correctly. If anyone can help me figure this out, I'd appreciate it.
Answer by UnityBrains · Oct 26, 2020 at 07:54 PM
Oh FFS. Of course JUST after I post my question I sort out what was happening. The CreateDelegate variant I'm using (`CreateDelegate(type, methodinfo)`) requires the calling function (in this case public void __Attack_SetFocused()
) to be static (it's the bottom variant on this page https://docs.microsoft.com/en-us/dotnet/api/system.delegate.createdelegate?view=netcore-3.1)
Right, either that or your delegate type requires a parameter of the type of your class (ButtonBehaviors).
Though I don't quite get why you use reflection for this? Since your method is public you can just assign it to your delegate You need a reference to your actual object instance that holds the method anyways.
$$anonymous$$ainly because I wanted to. I was annoyed by the lack of control I had over Unity's input system (an example being that you can't simply get the GameObject that triggers a OnPointerEnter event - or at least I couldn't find out how) and went down a rabbit hole. $$anonymous$$y scene is set up in such a way that all of the behaviors for my buttons are in a single script, which is attached to a GameObject. Using the UI system (button for example) I'd place that object into the "OnClick" trigger and select the necessary function to fire. That works fine, until you need to know which game object the event is firing from. Because the script is attached to a game object that ISN'T the one that's triggering the event, and there's no way to intercept events, it was difficult to deter$$anonymous$$e which button was triggering a "OnPointerEnter" event. With this system, I can easily track which object triggers which event.Though I don't quite get why you use reflection for this?