2

我创建了一个Direction像这样的枚举:

enum Direction { Up, Down, Left, Right }

现在这些方向在箭头键按下时被调用。我现在在我的KeyDown活动中是这样的:

if (e.KeyCode == Keys.Up)
{
    moveDirection = Direction.Up;
}
else if (e.KeyCode == Keys.Left)
{
    moveDireciton = Direction.Left;
}
else if ...

是否可以创建我自己的演员阵容(Direction)Keys.Up

我知道我可以创建一个静态方法static Direction CastKeysToDirection(Keys k);,我对此很好,但我只是好奇是否有一种方法可以在 c# 中实现我们自己的强制转换?

4

3 回答 3

7

如果您将枚举定义为

enum Direction {
    Up = Keys.Up,
    Down = Keys.Down,
    Left = Keys.Left,
    Right = Keys.Right
}

那么你的演员应该因为底层的数值与底层的数值相同(Direction)Keys.Up这一事实而起作用。Keys.UpDirection.Up

但遗憾的是explicit,您无法像在.impilcitenumclass

于 2013-02-26T14:41:32.353 回答
5

两种选择:

  • Just cast, if you're sure the values match (e.g. because you've created the Direction enum using Keys.Up etc as the values):

    moveDirection = (Direction) e.KeyCode;
    
  • Create a dictionary:

    private static readonly Dictionary<Keys, Direction> KeyToDirection =
        new Dictionary<Keys, Direction>
        {
            { Keys.Up, Direction.Up },
            { Keys.Left, Direction.Left },
            ...
        };
    

    Then just look up the key that's been pressed in the dictionary. (Use TryGetValue to look up a Keys - use the return value to detect keys which aren't present in the dictionary.)

The latter gives a more flexible approach, of course. It allows you to have multiple keys all mapping to the same direction, for example.

于 2013-02-26T14:41:42.173 回答
3

Implicit casting is not possible to enum, but you can use implicit operator in class implementation and then maybe use some benefits of polymorphism and encapsulation in further working (Included suggestion of @Chris Sinclair):

public abstract class Direction
{
    public static implicit operator Direction(Keys key)
    {
        switch (key)
        {
            case Keys.Up:
                return Up.Instance;
            case Keys.Down:
                return Down.Instance;
            case Keys.Left:
                return Left.Instance;
            case Keys.Right:
                return Right.Instance;
            default:
                throw new ArgumentOutOfRangeException();
        }
    }
}

public class Up : Direction
{
    private static readonly Up _Instance = new Up();
    public static Up Instance
    {
        get { return _Instance; }
    }

    private Up() {}
}

public class Down : Direction
{
    private static readonly Down _Instance = new Down();
    public static Down Instance
    {
        get { return _Instance; }
    }

    private Down() {}
}

public class Left : Direction
{
    private static readonly Left _Instance = new Left();
    public static Left Instance
    {
        get { return _Instance; }
    }

    private Left() {}
}

public class Right : Direction
{
    private static readonly Right _Instance = new Right();
    public static Right Instance
    {
        get { return _Instance; }
    }

    private Right() {}
}

The use case:

Keys key = Keys.Up;
Direction direction = key; // Legal

if (direction == Up.Instance)
{
    // Do something
}

// Or add abstract methods to Direction class and invoke direction.DoWork()
于 2013-02-26T14:48:45.370 回答