<tfoot id='fZpF5'></tfoot>

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

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

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

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

      1. 如何为具有空目标的实例方法创建委托?

        How to create a delegate to an instance method with a null target?(如何为具有空目标的实例方法创建委托?)

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

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

              1. <small id='Nm1Ga'></small><noframes id='Nm1Ga'>

                <tfoot id='Nm1Ga'></tfoot>
                • 本文介绍了如何为具有空目标的实例方法创建委托?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我注意到 Delegate 类有一个 Target 属性,它(大概)返回委托方法将在其上执行的实例.我想做这样的事情:

                  I've noticed that the Delegate class has a Target property, that (presumably) returns the instance the delegate method will execute on. I want to do something like this:

                  void PossiblyExecuteDelegate(Action<int> method)
                  {
                      if (method.Target == null)   
                      {
                          // delegate instance target is null
                          // do something
                      }
                      else
                      {
                           method(10);
                           // do something else
                      }
                  }
                  

                  调用它时,我想做类似的事情:

                  When calling it, I want to do something like:

                  class A
                  {
                      void Method(int a) {}
                  
                      static void Main(string[] args)
                      {
                          A a = null;
                          Action<int> action = a.Method;
                          PossiblyExecuteDelegate(action);
                      }
                  }
                  

                  但是当我尝试构造委托时,我得到了一个 ArgumentException(实例方法的委托不能有 null 'this').我想做的事是否可行,我该怎么做?

                  But I get an ArgumentException (Delegate to an instance method cannot have a null 'this') when I try to construct the delegate. Is what I want to do possible, and how can I do it?

                  推荐答案

                  啊哈!找到了!

                  您可以使用 CreateDelegate 重载,使用带有隐式this"第一个参数的委托:

                  You can create an open instance delegate using a CreateDelegate overload, using a delegate with the implicit 'this' first argument explicitly specified:

                  delegate void OpenInstanceDelegate(A instance, int a);
                  
                  class A
                  {
                      public void Method(int a) {}
                  
                      static void Main(string[] args)
                      {
                          A a = null;
                          MethodInfo method = typeof(A).GetMethod("Method");
                          OpenInstanceDelegate action = (OpenInstanceDelegate)Delegate.CreateDelegate(typeof(OpenInstanceDelegate), a, method);
                  
                          PossiblyExecuteDelegate(action);
                      }
                  }
                  

                  这篇关于如何为具有空目标的实例方法创建委托?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)
                  Parameter count mismatch with Invoke?(参数计数与调用不匹配?)
                  How to store delegates in a List(如何将代表存储在列表中)
                  How delegates work (in the background)?(代表如何工作(在后台)?)
                  C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)
                  Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)
                • <i id='NpBHI'><tr id='NpBHI'><dt id='NpBHI'><q id='NpBHI'><span id='NpBHI'><b id='NpBHI'><form id='NpBHI'><ins id='NpBHI'></ins><ul id='NpBHI'></ul><sub id='NpBHI'></sub></form><legend id='NpBHI'></legend><bdo id='NpBHI'><pre id='NpBHI'><center id='NpBHI'></center></pre></bdo></b><th id='NpBHI'></th></span></q></dt></tr></i><div id='NpBHI'><tfoot id='NpBHI'></tfoot><dl id='NpBHI'><fieldset id='NpBHI'></fieldset></dl></div>
                • <tfoot id='NpBHI'></tfoot>

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

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

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

                          <tbody id='NpBHI'></tbody>