我有一个WPF控件,文档很差.
在代码隐藏中,我想反思控件触发的事件,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);
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎在事件签名时有效(object,EventArgs)但在某些其他事件时无法绑定.
有没有办法做到这一点,而不必知道事件的签名?
您可以使用System.Linq.Expressions.Expression该类生成匹配事件签名的动态处理程序 - 您只需将调用放入其中Console.WriteLine.
该Expression.Lambda方法(提供了您需要的特定过载的链接)可用于生成Func<>或更可能Action<>的正确类型.
您反映事件的委托类型(抓住Invoke@Davio提到的方法)来拉出所有参数并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
}
Run Code Online (Sandbox Code Playgroud)
大纲 - 我们从一个具有一些事件的类型开始(我正在使用Action但它应该可以处理任何事件),并且有一个方法可以用来测试 - 触发所有那些拥有订阅者的事件.
然后到DynamicEventBinder类,它有两个方法:GetHandler- 获取特定类型的特定事件的处理程序; 并且SubscribeAllEvents为该类型的给定实例绑定所有这些事件 - 它只是遍历所有事件,调用AddEventHandler每个事件,调用GetHandler以获取处理程序.
这种GetHandler方法是肉和骨头的地方 - 并且完全按照我在轮廓中的建议.
委托类型具有Invoke由编译器编译到其中的成员,该成员镜像它可以绑定到的任何处理程序的签名.因此,我们反映该方法并获取它具有的任何参数,ParameterExpression为每个创建Linq 实例.命名参数是一个非常好的,这是重要的类型.
然后我们构建一个单行lambda,其主体基本上是:
Console.WriteLine("[event_name] was fired!");
Run Code Online (Sandbox Code Playgroud)
(请注意,事件的名称被拉入动态代码,并且就代码而言合并到一个常量字符串中)
当我们构建lambda时,我们还告诉Expression.Lambda方法我们打算构建的委托类型(直接绑定到事件的委托类型),并通过传递ParameterExpression我们之前创建的数组,它将生成一个具有那么多的方法参数.我们使用该Compile方法实际编译动态代码,这为我们提供了一个Delegate然后可以用作参数的方法AddEventHandler.
我真诚地希望这可以解释我们已经完成的工作 - 如果你没有使用表达式和动态代码,那么它可能会令人费解.事实上,我工作的一些人只是称这个伏都教.