-->

Subscribing an Action to any event type via reflec

2020-02-28 04:14发布

问题:

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.

回答1:

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);
  }


回答2:

How about this?

void Bar(object obj, EventInfo info)
{
    var parameters = info.EventHandlerType.GetMethod("Invoke").GetParameters()
        .Select(p => Expression.Parameter(p.ParameterType));

    var handler = Expression.Lambda(
        info.EventHandlerType,
        Expression.Call(
            Expression.Constant(obj), // obj is the instance on which Foo()
            "Foo",                    // will be called
            null
        ),
        parameters
    );
    info.AddEventHandler(obj, handler.Compile());
}