WCF枚举类型正确使用方法演示

开发 开发工具
WCF枚举类型的使用方式比较灵活,我们一般可以不同对其做任何的设置就能进行相关操作。接下来就一起来看看它的使用方式吧。

WCF开发工具的应用,首先需要初学者了解的就是在这一平台中的代码编写方式,这样我们才能利用这一平台来帮助我们打造一些特定的功能程序。在这里我们先来了解一下WCF枚举类型的使用情况。

通常情况下,我们无需做任何设置就可以使用WCF枚举类型。

  1. public enum DataType  
  2. {  
  3. A,  
  4. B,  
  5. C  
  6. }  
  7. [ServiceContract]  
  8. public interface IMyService  
  9. {  
  10. [OperationContract]  
  11. void Test(DataType d);  

客户端代理

  1. //------------------------------------------------
  2. // <auto-generated> 
  3. // 此代码由工具生成。  
  4. // 运行库版本:2.0.50727.42  
  5. //  
  6. // 对此文件的更改可能会导致不正确的行为,并且如果  
  7. // 重新生成代码,这些更改将会丢失。  
  8. // </auto-generated> 
  9. //------------------------------------------------
  10. namespace ConsoleApplication1.localhost  
  11. {  
  12. [GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]  
  13. [DataContractAttribute(Namespace = "...")]  
  14. public enum DataType : int  
  15. {  
  16. [EnumMemberAttribute()]  
  17. A = 0,  
  18. [numMemberAttribute()]  
  19. B = 1,  
  20. [EnumMemberAttribute()]  
  21. C = 2,  
  22. }  
  23. [GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]  
  24. [ServiceContractAttribute(ConfigurationName = 
    "ConsoleApplication1.localhost.IMyService")]  
  25. public interface IMyService  
  26. {  
  27. [OperationContractAttribute(Action = 
    "http://tempuri.org/IMyService/Test", ReplyAction = "...")]  
  28. void Test(DataType d);  
  29. }  

如果我们只想使用部分WCF枚举类型值时,可以使用 EnumMemberAttribute。

  1. [DataContract]  
  2. public enum DataType  
  3. {  
  4. [EnumMember]A,  
  5. B,  
  6. [EnumMember]C  

代理文件

  1. //--------------------------------------  
  2. // <auto-generated> 
  3. // 此代码由工具生成。  
  4. // 运行库版本:2.0.50727.42  
  5. //  
  6. // 对此文件的更改可能会导致不正确的行为,并且如果  
  7. // 重新生成代码,这些更改将会丢失。  
  8. // </auto-generated> 
  9. //----------------------------------------  
  10. namespace ConsoleApplication1.localhost  
  11. {  
  12. [GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]  
  13. [DataContractAttribute(Namespace = "...")]  
  14. public enum DataType : int  
  15. {  
  16. [EnumMemberAttribute()]  
  17. A = 0,  
  18. [EnumMemberAttribute()]  
  19. C = 2,  
  20. }  

我们还可以使用 EnumMemberAttribute.Value 为WCF枚举类型值设置一个 "固定" 的名字,这和前面我们提到的 ServiceContract / DataContract 版本问题类似。

  1. [DataContract]  
  2. public enum DataType  
  3. {  
  4. [EnumMember(Value="A")]Axxx,  
  5. B,  
  6. [EnumMember]C  

【编辑推荐】

  1. WCF服务实例管理模式之PreSession应用
  2. WCF控制服务对象释放特殊方式介绍
  3. WCF事务演示经典实例剖析
  4. 深入分析WCF事务投票实现方式
  5. WCF MSMQ队列基本概念简述
责任编辑:曹凯 来源: 豆豆网
相关推荐

2010-02-23 11:06:16

WCF可信赖会话

2010-03-01 17:39:07

WCF Address

2010-03-02 16:58:11

AJAX WCF服务项

2009-12-22 09:48:58

WCF枚举类型

2009-11-09 10:59:57

WCF回调

2011-04-27 16:38:31

投影机

2010-02-26 11:22:16

LitwareHR使用

2010-03-04 15:17:30

Python prin

2009-08-18 10:17:25

C#枚举类型

2009-12-21 18:32:22

关闭WCF链接

2010-02-22 14:28:35

WCF实现loadin

2009-12-02 14:50:25

PHP接口类inter

2020-11-20 14:27:08

PositionLay

2010-03-05 14:09:19

Python sys.

2010-01-20 17:47:54

VB.NET注释

2010-02-23 17:59:52

WSIT连接WCF

2009-11-26 18:49:54

PHP函数preg_s

2010-02-25 16:07:28

WCF REST

2010-02-23 17:05:38

2010-05-04 09:44:12

Oracle Trig
点赞
收藏

51CTO技术栈公众号