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,Action 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,invoke,parameters[0],parameters[1]),handler);
}
Action action = () => BM_21_Grad.LaunchMissle();
foreach (var eventInfo in form.GetType().GetEvents())
{
AddEventHandler(eventInfo,form,action);
}