繁体   English   中英

使用反射动态绑定到C#事件

[英]Dynamic Binding to C# Events Using Reflection

我有一个WPF控件,文档很差。

在代码隐藏中,我想反思控件使用GetType().GetEVents()触发的事件,并为每个事件添加一个处理程序, GetType().GetEVents()打印出事件的名称。

这将允许我看到与控件的交互实际正在做什么。

到目前为止,我有:

foreach (var e in GetType().GetEvents())
{
    var name = e.Name;
    var handler = new Action<object,object>( (o1,o2) =>Console.WriteLine(name));

    try
    {
        e.AddEventHandler(
                     this,
                     Delegate.CreateDelegate(
                               e.EventHandlerType,
                               handler.Target, 
                               handler.Method
                               ));
    }
    catch (Exception ex)
    {
        Console.WriteLine( "Failed to bind to event {0}", e.Name);
    }
}

当事件签名是(object,EventArgs)但在某些其他事件上无法绑定时,这似乎有效。

有没有办法做到这一点,而不必知道事件的签名?

您可以使用System.Linq.Expressions.Expression类生成与事件签名匹配的动态处理程序 - 您只需将调用放入Console.WriteLine

Expression.Lambda方法(提供了您需要的特定重载的链接)可用于生成正确类型的Func<>或更可能的Action<>

您反映了事件的委托类型(抓住@Davio提到的Invoke方法)来提取所有参数并为每个提供ParameterExpression ,以提供给lambda方法。

这是一个完整的解决方案,您可以粘贴到标准单元测试中,我将在后续编辑中进行解释:

public class TestWithEvents
{
  //just using random delegate signatures here
  public event Action Handler1;
  public event Action<int, string> Handler2;

  public void RaiseEvents(){
    if(Handler1 != null)
        Handler1();
    if(Handler2 != null)
      Handler2(0, "hello world");
  }
}

public static class DynamicEventBinder
{
  public static Delegate GetHandler(System.Reflection.EventInfo ev) {
    string name = ev.Name;
    // create an array of ParameterExpressions
    // to pass to the Expression.Lambda method so we generate
    // a handler method with the correct signature.
    var parameters = ev.EventHandlerType.GetMethod("Invoke").GetParameters().
      Select((p, i) => Expression.Parameter(p.ParameterType, "p" + i)).ToArray();

    // this and the Compile() can be turned into a one-liner, I'm just
    // splitting it here so you can see the lambda code in the Console
    // Note that we use the Event's type for the lambda, so it's tightly bound
    // to that event.
    var lambda = Expression.Lambda(ev.EventHandlerType,
      Expression.Call(typeof(Console).GetMethod(
        "WriteLine",
        BindingFlags.Public | BindingFlags.Static,
        null,
        new[] { typeof(string) },
        null), Expression.Constant(name + " was fired!")), parameters);

    //spit the lambda out (for bragging rights)
    Console.WriteLine(
      "Compiling dynamic lambda {0} for event \"{1}\"", lambda, name);
    return lambda.Compile();
  }

  //note - an unsubscribe might be handy - which would mean
  //caching all the events that were subscribed for this object
  //and the handler.  Probably makes more sense to turn this type
  //into an instance type that binds to a single object...
  public static void SubscribeAllEvents(object o){
    foreach(var e in o.GetType().GetEvents())
    {
      e.AddEventHandler(o, GetHandler(e));
    }
  }
}

[TestMethod]
public void TestSubscribe()
{
  TestWithEvents testObj = new TestWithEvents();
  DynamicEventBinder.SubscribeAllEvents(testObj);
  Console.WriteLine("Raising events...");
  testObj.RaiseEvents();
  //check the console output
}

大纲 - 我们从一个具有一些事件的类型开始(我正在使用Action但它应该可以处理任何事情),并且有一个方法可以用来测试所有那些拥有订阅者的事件。

然后到DynamicEventBinder类,它有两个方法: GetHandler - 获取特定类型的特定事件的处理程序; SubscribeAllEvents绑定该类型的给定实例的所有这些事件 - 它只是遍历所有事件,为每个事件调用AddEventHandler ,调用GetHandler来获取处理程序。

GetHandler方法是肉和骨头的位置 - 并且完全按照我在大纲中的建议进行操作。

委托类型具有由编译器编译到其中的Invoke成员,该成员镜像可以绑定到的任何处理程序的签名。 因此,我们反映该方法并获取它具有的任何参数,为每个创建Linq ParameterExpression实例。 命名参数是一个非常好的,这是重要的类型。

然后我们构建一个单行lambda,其主体基本上是:

 Console.WriteLine("[event_name] was fired!");

(请注意,事件的名称被拉入动态代码,并且就代码而言合并到一个常量字符串中)

当我们构建lambda时,我们还告诉Expression.Lambda方法我们打算构建的委托类型(直接绑定到事件的委托类型),并通过传递我们之前创建的ParameterExpression数组,它将生成一个方法它有很多参数。 我们使用Compile方法实际编译动态代码,它为我们提供了一个Delegate ,然后我们可以将其用作AddEventHandler的参数。

我真诚地希望这可以解释我们已经完成的工作 - 如果你没有使用表达式和动态代码,那么它可能会令人费解。 事实上,我工作的一些人只是称这个伏都教。

查看此处的示例: http//msdn.microsoft.com/en-us/library/system.reflection.eventinfo.addeventhandler.aspx

他们使用委托的Invoke方法来获取签名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM