枚举的界面

时间:2014-01-14 12:35:46

标签: c# interface enums remoting .net-remoting

我正在尝试将现有的C#项目移植到一个新的分布式项目,使用连接客户端和服务器组件的接口(它是一个用于大学练习的推箱子游戏)。

这是我的问题:

在一个组件中,说:Level.cs,我有:

Level.cs

namespace Sokoban
{
public enum MoveDirection { Right, Left, Down, Up }


public class Level
    {
    private MoveDirection sokoDirection = MoveDirection.Right;
    ...
    }
...
}

另一方面,接口:

Interfaces.cs:

namespace Interfaces
    {
    public class ILevel : MarshalByRefObject
        {
        ---> what should i place here??? <---
        }
    }

我试过以下选项没有成功:

Interfaces.cs:

public virtual enum MoveDirection { get; set; }
public virtual enum MoveDirection() { throw new NotImplementedException(); }

Level.cs:

public static enum MoveDirection { return { Right, Left... }; }
public static enum MoveDirection { get { return { right, Left, ...}; } }

2 个答案:

答案 0 :(得分:4)

public interface ILevel
{
     MoveDirection MoveDirection { get; set; }
}

和实施:

public class Level : MarshalByRefObject, ILevel
{
     public MoveDirection MoveDirection { get; set; }    
}

答案 1 :(得分:0)

我不知道我是否完全符合这个问题。 也许是这样的,你想为所有实现接口或使用类的类提供一个枚举。 我也试图通过界面提供枚举。但没有运气。

我是一个C#初学者,所以我可能很简单的方法是:
1.通过抽象类提供它。它可以工作,但如果它最初足以使用接口(写overwrite等),你会在类定义中获得一点开销。
2.我的解决方法是在单独的命名空间中提供枚举,包含您需要的所有地方
在你的情况下,例如:


Interfaces.cs

using Enumerations;
namespace Interfaces
{
    interface ILevel : MarshalByRefObject
    {
        int changeDirection(MoveDirection newDirection);
    }
}
namespace Enumerations
{
    public enum MoveDirection { Right, Left, Down, Up };
}