I want to access the event handler of a LinkButton
's Command
event, but the in the snippet bellow fieldInfo
is null.
LinkButton button = new LinkButton();
button.Command += (s, e) => { };
Type type = button.GetType();
EventInfo[] eventInfos = type.GetEvents(BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
foreach (EventInfo eventInfo in eventInfos)
if (eventInfo.Name == "Command")
{
// fieldInfo is null
FieldInfo fieldInfo = eventInfo.DeclaringType.GetField(eventInfo.Name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
Delegate delegates = (Delegate)fieldInfo.GetValue(button);
foreach (Delegate handler in delegates.GetInvocationList())
{
eventInfo.RemoveEventHandler(button, handler);
// or
eventInfo.AddEventHandler(button, handler);
// or do whatever with handler
}
}
Code snippet was inspired from this.
Why is fieldInfo
null ?
Is there another way to get the event handler of a LinkButton
's Command
event ?
Also Hans Passant sollution does not work on LinkButton
using Command
as the field name.
Aucun commentaire:
Enregistrer un commentaire