Consider:
someControl.Click += delegate { Foo(); };
The arguments of the event are irrelevant, I don't need them and I'm not interested in them. I just want Foo() to get called. There's no obvious way to do the same via reflection.
I'd like to translate the above into something along the lines of
void Foo() { /* launch missiles etc */ }
void Bar(object obj, EventInfo info)
{
Action callFoo = Foo;
info.AddEventHandler(obj, callFoo);
}
Also, I don't want to make the assumption that the type of object passed to Bar strictly adheres to the guidelines of using the EventHander(TArgs) signature for events. To put it simply, I'm looking for a way to subscribe an Action to any handler type; less simply, a way to convert the Action delegate into a delegate of the expected handler type.
static void AddEventHandler(EventInfo eventInfo, object item, Action action)
{
var parameters = eventInfo.EventHandlerType
.GetMethod("Invoke")
.GetParameters()
.Select(parameter => Expression.Parameter(parameter.ParameterType))
.ToArray();
var handler = Expression.Lambda(
eventInfo.EventHandlerType,
Expression.Call(Expression.Constant(action), "Invoke", Type.EmptyTypes),
parameters
)
.Compile();
eventInfo.AddEventHandler(item, handler);
}
static void AddEventHandler(EventInfo eventInfo, object item, Action<object, EventArgs> action)
{
var parameters = eventInfo.EventHandlerType
.GetMethod("Invoke")
.GetParameters()
.Select(parameter => Expression.Parameter(parameter.ParameterType))
.ToArray();
var invoke = action.GetType().GetMethod("Invoke");
var handler = Expression.Lambda(
eventInfo.EventHandlerType,
Expression.Call(Expression.Constant(action), invoke, parameters[0], parameters[1]),
parameters
)
.Compile();
eventInfo.AddEventHandler(item, handler);
}
Usage:
Action action = () => BM_21_Grad.LaunchMissle();
foreach (var eventInfo in form.GetType().GetEvents())
{
AddEventHandler(eventInfo, form, action);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With