C
C#2y ago
kaypee90

❔ How to add custom attributes to Winforms control events

I have added a custom attribute to a winforms control event but it is not being triggered. What is the right way to do that. [AttributeUsage(AttributeTargets.Method)] public class EventTimeAttribute : Attribute { private string EventName { get; set; } public EventTimeAttribute(string eventName) { EventName = eventName; Console.WriteLine($@"{DateTime.Now.ToShortDateString()}: {eventName}"); } } Using the attribute: [EventTime("lvAccounts_SelectedIndexChanged")] private void lvAccounts_SelectedIndexChanged(object sender, EventArgs e) { }
7 Replies
cptcrawler
cptcrawler2y ago
what are you trying to achieve ?
kaypee90
kaypee90OP2y ago
The attribute I sent it's just an example. What I am trying to do is post telemetry to an endpoint when an event is triggered. But I don't want to embed the code in the method I want to add it as an attribute
jcotton42
jcotton422y ago
Attributes are just metadata Nothing will happen unless you have something pulling the attribute via reflection, then acting on it
kaypee90
kaypee90OP2y ago
Interesting @jcotton42 so that means something like the Authorize attribute in ASP.net core gets it's attribute pulled by reflection before they work
jcotton42
jcotton422y ago
Yes
kaypee90
kaypee90OP2y ago
Thanks
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server