如何事件处理程序附加到使用反射的事件吗? [英] How to attach event handler to an event using reflection?

查看:86
本文介绍了如何事件处理程序附加到使用反射的事件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 EventInfo.AddEventHandler(...),它可以用来处理程序附加到事件方法。但是应该怎样做,如果我甚至不能定义事件处理程序的正确签名,如,我甚至不具备参考了处理程序?



预期事件参数

我会解释用正确的代码的问题。



//场景时,我有我的解决方案提供的一切,零反射情景。

 内部类SendCommentsManager 
{
公共无效自定义(IRFQWindowManager rfqWindowManager)
{
rfqWindowManager.SendComment + = HandleRfqSendComment;
}

私人无效HandleRfqSendComment(对象发件人,SendCommentEventArgs参数)
{
args.Cancel = TRUE;
}
}

现在,我希望通过使用来达到同样的目的反射。我已经能够算出来的,但是当我附上了代表对事件(使用的addEventHandler ),它抛出错误绑定到目标方法例外。



我明白此异常背后的原因,附加错误委托的事件。但必须有某种方式来实现这一目标。

 内部类SendCommentsManagerUsingReflection 
{
公共无效定制( IRFQWindowManager rfqWindowManager)
{
EventInfo eventInfo = rfqWindowManager.GetType()GetEvent(SendComment);
eventInfo.AddEventHandler(rfqWindowManager,
Delegate.CreateDelegate(eventInfo.EventHandlerType,这个HandleRfqSendComment));
//<<<<<<<<<<上面行是我错了>>>>>>>>> ;>>>>>
}

私人无效HandleRfqSendComment(对象发件人,对象参数)
{
型sendCommentArgsType = args.GetType();
的PropertyInfo cancelProperty = sendCommentArgsType.GetProperty(取消);
cancelProperty.SetValue(参数,真实,NULL);
}
}


解决方案

我想你的代码是失败,因为 HandleRfqSendComment 是私人的。相反,你可以直接创建一个委托给该方法,而不通过其更名为 createDelegate方法。那么你就需要委托转换为所需的类型,使用下面的方法:

 公共静态代表ConvertDelegate(代表originalDelegate,类型targetDelegateType)
{
返回Delegate.CreateDelegate(
targetDelegateType,
originalDelegate.Target,
originalDelegate.Method);
}

在你的代码,你可以按如下方式使用此方法:



  EventInfo eventInfo = rfqWindowManager.GetType()GetEvent(SendComment); 
动作<对象,对象>处理器= HandleRfqSendComment;
代表convertedHandler = ConvertDelegate(处理器,eventInfo.EventHandlerType);
eventInfo.AddEventHandler(rfqWindowManager,convertedHandler);


I know about EventInfo.AddEventHandler(...) method which can be used to attach handler to an event. But what should be done if i can not even define proper signature of the event handler, as in, i don't even have reference to the event args expected by the handler?

I will explain the problem with the proper code.

// Scenario when I have everything available in my solution, Zero Reflection Scenario.

internal class SendCommentsManager
{
    public void Customize(IRFQWindowManager rfqWindowManager)
    {
        rfqWindowManager.SendComment += HandleRfqSendComment;
    }

    private void HandleRfqSendComment(object sender, SendCommentEventArgs args)
    {
        args.Cancel = true;
    }
}

Now, I want to achieve the same objective by using reflection. I have been able to figure out most of it but when i attach a delegate to the event (using AddEventHandler) it throws "Error binding to target method." exception.

I understand the reason behind this exception, attaching a wrong delegate to an event. But there must be some way to achieve this.

 internal class SendCommentsManagerUsingReflection
 {
     public void Customize(IRFQWindowManager rfqWindowManager)
     {
         EventInfo eventInfo = rfqWindowManager.GetType().GetEvent("SendComment");
         eventInfo.AddEventHandler(rfqWindowManager, 
             Delegate.CreateDelegate(eventInfo.EventHandlerType, this, "HandleRfqSendComment"));
         //<<<<<<<<<<ABOVE LINE IS WHERE I AM GOING WRONG>>>>>>>>>>>>>>
     }

     private void HandleRfqSendComment(object sender, object args)
     {
         Type sendCommentArgsType = args.GetType();
         PropertyInfo cancelProperty = sendCommentArgsType.GetProperty("Cancel");
         cancelProperty.SetValue(args, true, null);
     }
 }

解决方案

I think your code is failing because the HandleRfqSendComment is private. Instead you could directly create a delegate to that method, without passing its name to CreateDelegate. You would then need to convert the delegate to the required type, using the following method :

public static Delegate ConvertDelegate(Delegate originalDelegate, Type targetDelegateType)
{
    return Delegate.CreateDelegate(
        targetDelegateType,
        originalDelegate.Target,
        originalDelegate.Method);
}

In your code, you could use this method as follows :

EventInfo eventInfo = rfqWindowManager.GetType().GetEvent("SendComment");
Action<object, object> handler = HandleRfqSendComment;
Delegate convertedHandler = ConvertDelegate(handler, eventInfo.EventHandlerType);
eventInfo.AddEventHandler(rfqWindowManager, convertedHandler);

这篇关于如何事件处理程序附加到使用反射的事件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆