Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用反射动态绑定到C#事件_C#_Events - Fatal编程技术网

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

使用反射动态绑定到C#事件,c#,events,C#,Events,我有一个WPF控件,文档很差 在codebehind中,我想回顾一下控件使用GetType().GetEVents()触发的事件,并为每个事件添加一个处理程序,该处理程序只打印出事件的名称 这将允许我查看与控件交互的实际操作 到目前为止,我已经: foreach (var e in GetType().GetEvents()) { var name = e.Name; var handler = new Action<object,object>( (o1,o2) =

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

在codebehind中,我想回顾一下控件使用
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);
    }
}
foreach(GetType().GetEvents()中的变量e)
{
变量名称=e.名称;
var handler=newaction((o1,o2)=>Console.WriteLine(name));
尝试
{
e、 加法器(
这
Delegate.CreateDelegate(
e、 EventHandlerType,
处理程序。目标,
方法
));
}
捕获(例外情况除外)
{
WriteLine(“未能绑定到事件{0}”,e.Name);
}
}
当事件签名为
(object,EventArgs)
时,它似乎起作用,但在某些其他事件上无法绑定


有没有一种方法可以在不一定知道事件签名的情况下执行此操作?

您可以使用
System.Linq.Expressions.Expression
类来生成与事件签名匹配的动态处理程序,只需在其中调用
Console.WriteLine

该方法(提供了指向所需特定重载的链接)可用于生成正确类型的
Func
或更可能的
Action

您可以反映事件的委托类型(抓取@Davio提到的
Invoke
方法)以提取所有参数,并为每个参数创建
ParameterExpression
s以提供给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
}
(这里请注意,事件的名称被拉入动态代码中,并就代码而言被合并到一个常量字符串中)

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


我真诚地希望这能解释我们所做的事情——如果你还没有使用过表达式和动态代码,那么它可能会让人心烦意乱。事实上,与我共事的一些人干脆称之为伏都教。

看看这里的例子:


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

这听起来很有趣,但我正在努力实现!我将用type+和event组合一个测试供您使用。。。可能需要半个小时…我在EventInfo对象上调用AddHandler,得到的结果是:“类型为'System.Action'2[System.object,System.Windows.Input.MouseButtonEventArgs]'的对象无法转换为'System.Windows.Input.MouseButtonEventHandler'类型。”!我就快到了,但我没有将事件处理程序类型传递到Expression.Lambda函数中。谢谢你的帮助。
 Console.WriteLine("[event_name] was fired!");