添加 WCF 服务和 Windows Phone 7 应用程序中使用的枚举类型
我正在开发一个使用 WCF 服务的 Windows Phone 7 应用程序。
我需要在两个项目上使用以下代码:
public enum GameType
{
MonoPlayer = 1,
MultiPlayer = 2
}
我确信我不能在两个项目上定义这个枚举,所以我发现我需要找到另一个解决方案。
我想我需要使用第三个项目,我必须在其中放置枚举。
你有更好的方法吗?
I'm developing a Windows Phone 7 app that uses a WCF service.
I need to use on both projects the following code:
public enum GameType
{
MonoPlayer = 1,
MultiPlayer = 2
}
I'm sure I must not define this enum on both projects, so I figure out that I need to find another solution.
I think I need to use a third project where I have to put enum.
Do you have a better approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
WCF 使用契约,因此必须将枚举装饰为契约。
例如,您可以:
将这个枚举文件放在一个单独的项目中,以便客户端和 WCF 服务可以共享它。
然后,在服务契约(即 WCF 服务的接口)中,您必须将枚举声明为“已知类型”,如下所示:
应该可以了!
WCF uses contracts, so the enum must be decorated as a contract.
For instance, you can have:
You put this enum file in a separate project, so that it can be shared by client and WCF service.
Then, in the service contract (i.e., the interface of your WCF service), you must declare the enum as a "known type", like so:
That should do it!
我通常做的是有一个单独的项目,其中包含您所引用的域逻辑类型(例如 GameType)的所有类和枚举,这些类和枚举在其他项目之间共享,并将其命名为:Xpto.Common
然后我在我的两个项目中引用该公共项目。这使得类和枚举可以重用并保持事物的组织性。
What I normally do is have a separate project that has all the classes and enumerations that have the kind of domain logic you are refering to (like GameType) that is shared across other projects and call it: Xpto.Common
I then reference that common project in both my projects. That makes the classes and enumerations reusable and keeps things organized.
显然,可重用的类必须放入单独的项目中。
Obviously, reusable classes must be put into a separate project.