❔ 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
what are you trying to achieve ?
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
Attributes are just metadata
Nothing will happen unless you have something pulling the attribute via reflection, then acting on it
Interesting @jcotton42 so that means something like the Authorize attribute in ASP.net core gets it's attribute pulled by reflection before they work
Yes
Thanks
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.