How do delegates work
I have a little problem understanding what type i need to pass a delegate to AddEventHandler because i'm getting the function from reflection and MethodInfo.CreateDelegate tells me i need to pass a type which i don't understand what would that type be for or what derives from a delegate exactly
13 Replies
also the delegate must be async since it's a Discord.Net event
That typeof(void) in there i just put because at this point i tried everything lol, i don't know what type to put in there
i mean in this case it should be some type related to any method, so i don't actually know how to do this.
You need to declare a delegate that matches your methods' signature
method.CreateDelegate<YourDelegate>()
what is the event you're trying to add to?
the event is whatever it gets from the string passed in the attribute constructor arg
well i'll give an example
this is what i don't understand basically
if you had
typeof(Func<int, Task>)
is what you pass to CreateDelegatei see, so that's the function return type then
its not its return type, thats how you define events
Func<T1, TRet>
is a delegate type
it seems like you have an eventinfo therewhich means you can get that type using https://learn.microsoft.com/en-us/dotnet/api/system.reflection.eventinfo.eventhandlertype?view=net-6.0#system-reflection-eventinfo-eventhandlertype
EventInfo.EventHandlerType Property (System.Reflection)
Gets the Type object of the underlying event-handler delegate associated with this event.
so EventInfo.GetType?
no
EventInfo.EventHandlerType
you're getting the EventInfo from
GetType().GetEvent(...)
ok, i'l try to pass (use) that
i'l split stuff in variables and pass (use) that, thanks
Cannot bind to the target method because its signature is not compatible with t
hat of the delegate type.
this means that the function the event expects is not compatible as far as i know?
@Windows10CE
ah it works, it just had to be static, my bad, thanks a lot