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

      <bdo id='NiRnr'></bdo><ul id='NiRnr'></ul>
  • <i id='NiRnr'><tr id='NiRnr'><dt id='NiRnr'><q id='NiRnr'><span id='NiRnr'><b id='NiRnr'><form id='NiRnr'><ins id='NiRnr'></ins><ul id='NiRnr'></ul><sub id='NiRnr'></sub></form><legend id='NiRnr'></legend><bdo id='NiRnr'><pre id='NiRnr'><center id='NiRnr'></center></pre></bdo></b><th id='NiRnr'></th></span></q></dt></tr></i><div id='NiRnr'><tfoot id='NiRnr'></tfoot><dl id='NiRnr'><fieldset id='NiRnr'></fieldset></dl></div>
  • <tfoot id='NiRnr'></tfoot>
    <legend id='NiRnr'><style id='NiRnr'><dir id='NiRnr'><q id='NiRnr'></q></dir></style></legend>
      1. 为什么不能将 .NET 委托声明为静态的?

        Why can a .NET delegate not be declared static?(为什么不能将 .NET 委托声明为静态的?)

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

            • <bdo id='YVxHP'></bdo><ul id='YVxHP'></ul>
                <tfoot id='YVxHP'></tfoot>
              • <small id='YVxHP'></small><noframes id='YVxHP'>

                  <tbody id='YVxHP'></tbody>

                1. 本文介绍了为什么不能将 .NET 委托声明为静态的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  当我尝试编译以下内容时:

                  When I try to compile the following:

                  public static delegate void MoveDelegate (Actor sender, MoveDirection args);
                  

                  我收到一个错误消息:修饰符 'static' 对该项目无效."

                  I receive, as an error: "The modifer 'static' is not valid for the this item."

                  我在一个单例中实现这个,有一个单独的类调用委托.问题是当我在另一个类中使用单例实例来调用委托时(从标识符,而不是类型),无论出于何种原因,我都不能这样做,即使我声明委托是非静态的.显然,只有当且仅当委托是静态的时,我才能通过类型直接引用它.

                  I'm implementing this within a singleton, with a separate class which calls the delegate. The problem is that when I use the singleton instance within the other class to call the delegate (from the identifier, not the type), I can't do that for whatever reason, even when I declare the delegate non-static. Obviously, I can only refer to it via the type directly if and only if the delegate is static.

                  这背后的原因是什么?我正在使用 MonoDevelop 2.4.2.

                  What is the reasoning behind this? I am using MonoDevelop 2.4.2.

                  更新

                  在尝试使用以下代码的建议之一后:

                  After trying one of the suggestions with the following code:

                  public void Move(MoveDirection moveDir)
                  {
                      ProcessMove(moveDir);
                  }
                  
                  public void ProcessMove(MoveDirection moveDir)
                  {
                      Teleporter.MoveMethod mm = new Teleporter.MoveMethod(Move); 
                      moveDelegate(this, moveDir);
                  }
                  

                  我收到一个处理错误,指出 MoveMethod 必须是一个类型,而不是一个标识符.

                  I've received a processing error, which states that the MoveMethod must be a type, and not an identifier.

                  推荐答案

                  试试这个:

                  public delegate void MoveDelegate(object o);
                  public static MoveDelegate MoveMethod;
                  

                  所以方法变量可以定义为静态的.static 关键字对于 delegate 定义没有意义,就像 enumconst 定义一样.

                  So the method-variable can be defined static. The keyword static has no meaning for the delegate definition, just like enum or const definitions.

                  如何分配静态方法字段的示例:

                  An example of how to assign the static method-field:

                  public class A
                  {
                    public delegate void MoveDelegate(object o);
                    public static MoveDelegate MoveMethod;
                  }
                  
                  public class B
                  {
                    public static void MoveIt(object o)
                    {
                      // Do something
                    }    
                  }
                  
                  public class C
                  {
                    public void Assign()
                    {
                      A.MoveMethod = B.MoveIt;
                    }
                  
                    public void DoSomething()
                    {
                      if (A.MoveMethod!=null)
                        A.MoveMethod(new object()); 
                    }
                  }
                  

                  这篇关于为什么不能将 .NET 委托声明为静态的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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() 和泛型:错误绑定到目标方法)
                    <tbody id='Ff6FV'></tbody>
                2. <i id='Ff6FV'><tr id='Ff6FV'><dt id='Ff6FV'><q id='Ff6FV'><span id='Ff6FV'><b id='Ff6FV'><form id='Ff6FV'><ins id='Ff6FV'></ins><ul id='Ff6FV'></ul><sub id='Ff6FV'></sub></form><legend id='Ff6FV'></legend><bdo id='Ff6FV'><pre id='Ff6FV'><center id='Ff6FV'></center></pre></bdo></b><th id='Ff6FV'></th></span></q></dt></tr></i><div id='Ff6FV'><tfoot id='Ff6FV'></tfoot><dl id='Ff6FV'><fieldset id='Ff6FV'></fieldset></dl></div>
                  • <bdo id='Ff6FV'></bdo><ul id='Ff6FV'></ul>

                          <tfoot id='Ff6FV'></tfoot>

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

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