Discussion:
Dyamically add control event from method name as string
(too old to reply)
x***@y.com
2016-09-02 05:14:28 UTC
Permalink
To add event to a control, say a button, you do:

button1.Click += new EventHandler(button1_Click);

where button1_Click is the method to handle the event.

What if I have the name of the method as a string, "button1_Click".

How do I add the event from this string?
x***@y.com
2016-09-02 05:36:57 UTC
Permalink
Post by x***@y.com
button1.Click += new EventHandler(button1_Click);
where button1_Click is the method to handle the event.
What if I have the name of the method as a string, "button1_Click".
How do I add the event from this string?
I found an example that does this using reflection.

EventInfo ClickEvent = button1.GetType().GetEvent("Click");
Type MyDelegate = ClickEvent.EventHandlerType;
MethodInfo MyHandler = this.GetType().GetMethod("button1_Click",
BindingFlags.NonPublic |
BindingFlags.Instance);
Delegate del = Delegate.CreateDelegate(MyDelegate, this, MyHandler);
MethodInfo addHandler = ClickEvent.GetAddMethod();
Object[] addHandlerArgs = { del };
addHandler.Invoke(button1, addHandlerArgs);

That's a lot of code.
Is there a shorter way to do this?
x***@y.com
2016-09-02 06:46:46 UTC
Permalink
Post by x***@y.com
Post by x***@y.com
button1.Click += new EventHandler(button1_Click);
where button1_Click is the method to handle the event.
What if I have the name of the method as a string, "button1_Click".
How do I add the event from this string?
I found an example that does this using reflection.
EventInfo ClickEvent = button1.GetType().GetEvent("Click");
Type MyDelegate = ClickEvent.EventHandlerType;
MethodInfo MyHandler = this.GetType().GetMethod("button1_Click",
BindingFlags.NonPublic |
BindingFlags.Instance);
Delegate del = Delegate.CreateDelegate(MyDelegate, this, MyHandler);
MethodInfo addHandler = ClickEvent.GetAddMethod();
Object[] addHandlerArgs = { del };
addHandler.Invoke(button1, addHandlerArgs);
That's a lot of code.
Is there a shorter way to do this?
After further reading I found a shorter version.

EventInfo ClickEvent = button1.GetType().GetEvent("Click");
Type handlerType = ClickEvent.EventHandlerType;
Delegate del = Delegate.CreateDelegate(handlerType, this,
"button1_Click");
ClickEvent.AddEventHandler(button1, del);

I don't suppose you can get a version shorter than this.
Marcel Mueller
2016-09-03 08:46:59 UTC
Permalink
Post by x***@y.com
EventInfo ClickEvent = button1.GetType().GetEvent("Click");
Type handlerType = ClickEvent.EventHandlerType;
Delegate del = Delegate.CreateDelegate(handlerType, this,
"button1_Click");
ClickEvent.AddEventHandler(button1, del);
I don't suppose you can get a version shorter than this.
There is no need to register the handler by reflection.

button1.Click +=
(EventHandler)Delegate.CreateDelegate(typeof(EventHandler), this,
"button1_Click")


However, using "button1_Click" to identify the method is still bad practice.
new EventHandler(button1_Click) would do the job as well. See other post.


Marcel
x***@y.com
2016-09-03 16:49:49 UTC
Permalink
On Sat, 03 Sep 2016 10:46:59 +0200, Marcel Mueller
Post by Marcel Mueller
Post by x***@y.com
EventInfo ClickEvent = button1.GetType().GetEvent("Click");
Type handlerType = ClickEvent.EventHandlerType;
Delegate del = Delegate.CreateDelegate(handlerType, this,
"button1_Click");
ClickEvent.AddEventHandler(button1, del);
I don't suppose you can get a version shorter than this.
There is no need to register the handler by reflection.
button1.Click +=
(EventHandler)Delegate.CreateDelegate(typeof(EventHandler), this,
"button1_Click")
However, using "button1_Click" to identify the method is still bad practice.
new EventHandler(button1_Click) would do the job as well. See other post.
Marcel
Thanks for the info.

I wanted to see if I can add event handler to a control based on the
handler name as string.

Marcel Mueller
2016-09-03 08:42:16 UTC
Permalink
Post by x***@y.com
button1.Click += new EventHandler(button1_Click);
where button1_Click is the method to handle the event.
What if I have the name of the method as a string, "button1_Click".
How do I add the event from this string?
You already discovered that your problem can be solved by reflection,
but the more interesting question is how you get the name of a method
into a string. There are only two options: either you have a hard coded
string in your code or you already used reflection for this purpose
(including the nameof operator of .NET 4.6).
/Both is bad practice./

You should not abuse a string to represent a method reference. The
native type for method references are delegates. In this case the type
EventHandler.
There is no need that creation of the delegate EventHandler occurs in
the same line that the event registration. You can store the
EventHandler into a variable or field at one code location - where your
method name currently comes from - and register one of the created
delegates as event handler at another location - where you use
reflection now.


Marcel
Loading...