C
C#

help

How do delegates work

Mmemw9/23/2022
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 also the delegate must be async since it's a Discord.Net event
foreach (MethodInfo method in cConfig.EventsAssembly.GetTypes().SelectMany(t => t.GetMethods()).Where(m => m.GetCustomAttributes(typeof(EventAttribute), false).Length > 0).ToArray())
{
CustomAttributeData catData = method.GetCustomAttributesData().First(a => a.AttributeType.Name == "EventAttribute");
GetType().GetEvent((string)catData.ConstructorArguments.First().Value!)!.AddEventHandler(this, method.CreateDelegate(typeof(void)));
}
foreach (MethodInfo method in cConfig.EventsAssembly.GetTypes().SelectMany(t => t.GetMethods()).Where(m => m.GetCustomAttributes(typeof(EventAttribute), false).Length > 0).ToArray())
{
CustomAttributeData catData = method.GetCustomAttributesData().First(a => a.AttributeType.Name == "EventAttribute");
GetType().GetEvent((string)catData.ConstructorArguments.First().Value!)!.AddEventHandler(this, method.CreateDelegate(typeof(void)));
}
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.
KKouhai /人◕ ‿‿ ◕人\9/23/2022
You need to declare a delegate that matches your methods' signature method.CreateDelegate<YourDelegate>()
AAaron9/23/2022
what is the event you're trying to add to?
Mmemw9/23/2022
the event is whatever it gets from the string passed in the attribute constructor arg
AAaron9/23/2022
well i'll give an example
Mmemw9/23/2022
this is what i don't understand basically
AAaron9/23/2022
if you had
public event Func<int, Task> SomeEvent;
public event Func<int, Task> SomeEvent;
typeof(Func<int, Task>) is what you pass to CreateDelegate
Mmemw9/23/2022
i see, so that's the function return type then
AAaron9/23/2022
its not its return type, thats how you define events Func<T1, TRet> is a delegate type it seems like you have an eventinfo there
Mmemw9/23/2022
so EventInfo.GetType?
AAaron9/23/2022
no EventInfo.EventHandlerType you're getting the EventInfo from GetType().GetEvent(...)
Mmemw9/23/2022
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

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
Access Azure Table Storage from Asp.net 6.0 coreGetting the following in error when attempting to connect to Azure Table Storage. Am using Visual StExiting if loop inside of a while statement and starting overin my else if statement, how do i make it start at the beginning of the while loop if number is loweHow do i update a value and + add int values constantlyI have an int value and i wanna add numbers to it and i want it to update. for example if i add 10 What is required for indexing with ranges? [Answered]What exactly is required for indexing with a range (ex. `obj[..3]`) to be supported on a class? JustSendKeys() Access DeniedI'm using UiPath's SendKeys activity which maps to the SendKeys() function. For some reason, I am exDownload from URI in model errorOther problem was solved, now I've this one xD. Trying to do the following: ``` public statichow do i pick a random value from an arrayim coding a simple random name picker but there something wrong with the codehow do i store multiple values in one string?im trying to build a very simple random name picker but i forgot how to store muliple values in one Implementing an inventory system with right click actionsThis is really a hypothetical question as I'm not working on anything like this at the moment, but IWhy only one EF Core Global Query Filter work but others are notHi, I'm working with `EF Core 6`, recently I want to use the `Global Query Filters`. So in `OnModelC.net core webapi2 API key authorizationI was wondering what the absolute best practice was to implement API key authorization for your contHow to create css file linked to a razor page ?I see that the default template have the index.razor attached with a isolated css how can we do thaXamarin - Displaying JSONI'm trying to do a simple application to get a JSON and display the values on a collection. I have aOptimizing Memory UsageI've a tool that basically takes a bunch of files and converts them to a GIF. The way it works is tC-sharp Video Extraction LibrariesI am looking for a good, preferably open-source library for importing video files (e.g. AVI) into anAPI Design of login and refresh JWT token endpoint questionsI'm designing Login and refresh token and I have several questions. This is what I return on any refSystem.CommandLine and injection of IOptions (not Option!) typeI'm trying to work out how to use the hosting and dependency injection parts of System.CommandLine tInterface that is implemented to multiple classes (different types of objects)Method in class1: ```public Employee CreateEmployee(EmployeeCreateModel1 model) { .... }``` Method Wpf how to send custom event (or RoutedEvent) from parent to all childs that implement the handler.Hi guys, as the title says, i want to send an event from parent control to all childrens that impleMinimal API's WithNameWhat does `WithName` do? ```cs app.MapGet("ftx", FtxSubscribe).WithName("FtxUserTradesSubscription"