Why can a .NET delegate not be declared static?

When I try to compile the following:

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

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.

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

update

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

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


Try this:

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

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

You are declaring a delegate type. It doesn't make any sense to declare it as static . You could declare an instance of your delegate type as static , though.

public delegate void BoringDelegate();


internal class Bar {
    public static BoringDelegate NoOp;
    static Bar() {
        NoOp = () => { };
    }
}

A delegate declaration basically declares a method signature , which only includes information about its parameters and return type. And since the same delegate can point to both static and instance methods, it doesn't make sense to make the method signature itself static or instance.

Once you have declared your delegate as:

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

it means that any delegate of this type must point to a method which accepts one Actor parameter, one MoveDirection parameter, and returns void , regardless of whether the method is static or instance. You can declare the delegate at namespace scope, or inside a class (just like you would declare a nested class).

So after declaring the MoveDelegate somewhere, you can the create fields and variables of that type:

private MoveDelegate _myMoveDelegate;

and remember that the method should have a matching signature:

// parameters and return type must match!
public void Move(Actor actor, MoveDirection moveDir)
{
    ProcessMove (moveDir);
}

public static void MoveStatic(Actor actor, MoveDirection moveDir)
{
    ProcessMove (moveDir);
}

then you can assign this method to a delegate at some other place:

private void SomeOtherMethod()
{
     // get a reference to the Move method
     _myMoveDelegate = Move;

     // or, alternatively the longer version:
     // _myMoveDelegate = new MoveDelegate(Move);

     // works for static methods too
     _myMoveDelegate = MoveStatic;

     // and then simply call the Move method indirectly
     _myMoveDelegate(someActor, someDirection);
}

It is useful to know that .NET (starting from version v3.5) provides some predefined generic delegates ( Action and Func ) which can be used instead of declaring your own delegates :

// you can simply use the Action delegate to declare the
// method which accepts these same parameters
private Action<Actor, MoveDirection> _myMoveDelegate;

Using those delegates is IMHO more readable, since you can immediately identify parameters' signature from looking at the delegate itself (while in your case one needs to look for the declaration).

链接地址: http://www.djcxy.com/p/82118.html

上一篇: 在为.NET选择依赖注入框架时应该考虑什么

下一篇: 为什么.NET委托不能被声明为静态?