<tfoot id='hNZxR'></tfoot>

    <small id='hNZxR'></small><noframes id='hNZxR'>

        <bdo id='hNZxR'></bdo><ul id='hNZxR'></ul>

    1. <i id='hNZxR'><tr id='hNZxR'><dt id='hNZxR'><q id='hNZxR'><span id='hNZxR'><b id='hNZxR'><form id='hNZxR'><ins id='hNZxR'></ins><ul id='hNZxR'></ul><sub id='hNZxR'></sub></form><legend id='hNZxR'></legend><bdo id='hNZxR'><pre id='hNZxR'><center id='hNZxR'></center></pre></bdo></b><th id='hNZxR'></th></span></q></dt></tr></i><div id='hNZxR'><tfoot id='hNZxR'></tfoot><dl id='hNZxR'><fieldset id='hNZxR'></fieldset></dl></div>

        <legend id='hNZxR'><style id='hNZxR'><dir id='hNZxR'><q id='hNZxR'></q></dir></style></legend>

        如何使用反射将事件处理程序附加到事件?

        时间:2023-09-16
          <i id='QyImY'><tr id='QyImY'><dt id='QyImY'><q id='QyImY'><span id='QyImY'><b id='QyImY'><form id='QyImY'><ins id='QyImY'></ins><ul id='QyImY'></ul><sub id='QyImY'></sub></form><legend id='QyImY'></legend><bdo id='QyImY'><pre id='QyImY'><center id='QyImY'></center></pre></bdo></b><th id='QyImY'></th></span></q></dt></tr></i><div id='QyImY'><tfoot id='QyImY'></tfoot><dl id='QyImY'><fieldset id='QyImY'></fieldset></dl></div>

          <small id='QyImY'></small><noframes id='QyImY'>

          <legend id='QyImY'><style id='QyImY'><dir id='QyImY'><q id='QyImY'></q></dir></style></legend>

                <bdo id='QyImY'></bdo><ul id='QyImY'></ul>

                1. <tfoot id='QyImY'></tfoot>
                    <tbody id='QyImY'></tbody>
                2. 本文介绍了如何使用反射将事件处理程序附加到事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我知道 EventInfo.AddEventHandler(...) 方法,该方法可用于将处理程序附加到事件.但是,如果我什至无法定义事件处理程序的正确签名,例如,我什至没有对处理程序预期的事件参数的引用,该怎么办?

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

                  现在,我想通过使用反射来实现相同的目标.我已经能够弄清楚其中的大部分,但是当我将委托附加到事件时(使用 AddEventHandler)它会抛出 "Error binding to target method." 异常.

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

                  推荐答案

                  我认为您的代码失败了,因为 HandleRfqSendComment 是私有的.相反,您可以直接为该方法创建一个委托,而无需将其名称传递给 CreateDelegate.然后,您需要使用以下方法将委托转换为所需的类型:

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

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

                  上一篇:在 C# 中,为什么我不能传递另一个类的 EventHandler 引用,我该如何绕过它? 下一篇:动态创建的链接按钮列表,链接按钮不回发

                  相关文章

                3. <legend id='bxvD4'><style id='bxvD4'><dir id='bxvD4'><q id='bxvD4'></q></dir></style></legend>

                  1. <tfoot id='bxvD4'></tfoot>

                      • <bdo id='bxvD4'></bdo><ul id='bxvD4'></ul>

                    1. <i id='bxvD4'><tr id='bxvD4'><dt id='bxvD4'><q id='bxvD4'><span id='bxvD4'><b id='bxvD4'><form id='bxvD4'><ins id='bxvD4'></ins><ul id='bxvD4'></ul><sub id='bxvD4'></sub></form><legend id='bxvD4'></legend><bdo id='bxvD4'><pre id='bxvD4'><center id='bxvD4'></center></pre></bdo></b><th id='bxvD4'></th></span></q></dt></tr></i><div id='bxvD4'><tfoot id='bxvD4'></tfoot><dl id='bxvD4'><fieldset id='bxvD4'></fieldset></dl></div>

                      <small id='bxvD4'></small><noframes id='bxvD4'>