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

  • <small id='RwC2d'></small><noframes id='RwC2d'>

        <legend id='RwC2d'><style id='RwC2d'><dir id='RwC2d'><q id='RwC2d'></q></dir></style></legend><tfoot id='RwC2d'></tfoot>

      1. 在动态事件处理程序中引用“this"

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

            <legend id='WWKTO'><style id='WWKTO'><dir id='WWKTO'><q id='WWKTO'></q></dir></style></legend>
              <tfoot id='WWKTO'></tfoot>

                <tbody id='WWKTO'></tbody>
                <bdo id='WWKTO'></bdo><ul id='WWKTO'></ul>

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

                • 本文介绍了在动态事件处理程序中引用“this"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在我的myClass"类中,我使用 Reflection.Emit 为 myClass 类的成员之一动态编写事件处理程序.

                  In my 'myClass' class, I am using Reflection.Emit to dynamically write an event handler for one of the myClass class' members.

                  我已经成功完成了.

                  现在,我想修改事件处理程序以调用 myClass 类中的实例方法之一.

                  Now, I want to modify the event handler to call one of the instance methods in the myClass class.

                  但是,我无法弄清楚如何使用 Reflection.Emit 将对this"的引用推送到 MSIL 堆栈.在事件处理程序中,Ldarg_0 不是对this"的引用,而是事件处理程序的第一个参数.

                  However, I cannot figure out how to push a reference to 'this' onto the MSIL stack using Reflection.Emit. Within the event handler, Ldarg_0 is not a reference to 'this', but rather the first parameter of the event handler.

                  有谁知道如何在堆栈上推送对this"的引用,以便我可以调用实例方法.例如,这就是我希望 c# 代码的样子:

                  Does anyone know how to push a reference to 'this' on the stack so that I can call an instance method. For example, this is what I would want the c# code to look like:

                  public class myClass
                  {
                  private myObj1 obj1;
                  public myClass() {
                     this.init();
                  }
                  
                  private void init()
                  {
                     obj1.myEvent += new myEvent_EventHandler(theHandler);
                  }
                  
                  private void theHandler(myObj2 obj2, myObj3 obj3)
                  {
                     // this is the part I'm having trouble with
                     this.myFunction(obj2);
                  }
                  
                  private void myFunction(myObj2 obj2)
                  {
                     // irrelevant
                  }
                  }
                  

                  谢谢!

                  推荐答案

                  当你使用 Reflection.Emit (这里我假设 DynamicMethod),可以选择生成代码的第一个参数是什么,它可以由委托隐式传递,如下所示:

                  When you use Reflection.Emit (and I'm presuming DynamicMethod here), you get to choose what the first argument to the generated code will be, and it can be passed implicitly by the delegate, like this:

                  using System;
                  using System.Reflection.Emit;
                  
                  public class App
                  {
                      static void Main()
                      {
                          DynamicMethod m = new DynamicMethod("test", typeof(void),
                              new[] { typeof(App), // <-- type of first argument, your choice
                                  typeof(string) });
                  
                          var cg = m.GetILGenerator();
                  
                          cg.Emit(OpCodes.Ldarg_0);
                          cg.Emit(OpCodes.Ldarg_1);
                          cg.EmitCall(OpCodes.Call,
                              typeof(App).GetMethod("ShowString"), null);
                  
                          cg.Emit(OpCodes.Ret);
                  
                          Action<string> d = (Action<string>) 
                              m.CreateDelegate(typeof(Action<string>), 
                              new App()); // <-- this is the first argument, *your* choice
                  
                          MyEvent += d;
                  
                          // Trigger event
                          MyEvent("Hello there");
                      }
                  
                      static event Action<string> MyEvent;
                  
                      public void ShowString(string s)
                      {
                          Console.WriteLine(s);
                      }
                  }
                  

                  这篇关于在动态事件处理程序中引用“this"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:C# 到达 webbrowser 中的单击按钮? 下一篇:C# - 在 PowerStatus 更改时引发事件

                  相关文章

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

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

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

                      <legend id='CbFWk'><style id='CbFWk'><dir id='CbFWk'><q id='CbFWk'></q></dir></style></legend>
                      <tfoot id='CbFWk'></tfoot>