WCF集合元素基本概念详解
创始人
2024-06-16 18:41:26
0

WCF开发框架中有很多集合元素,在这里我们会为大家详细介绍这些WCF集合元素的定义方法,和相关使用技巧,希望对大家有所帮助。#t#

WCF集合元素类的定义如下:

  1. public enum FileType  
  2. {  
  3. TXT,DOC,HTML,OTHER  
  4. }  
  5. [DataContract]  
  6. public class Document  
  7. {  
  8. private string m_localPath;  
  9. private string m_fileName;  
  10. private FileType m_fileType;   
  11. [DataMember]  
  12. public string LocalPath  
  13. {  
  14. get { return m_localPath; }  
  15. set { m_localPath = value; }  
  16. }  
  17. [DataMember]  
  18. public string FileName  
  19. {  
  20. get { return m_fileName; }  
  21. set { m_fileName = value; }  
  22. }  
  23. [DataMember]  
  24. public FileType FileType  
  25. {  
  26. get { return m_fileType; }  
  27. set { m_fileType = value; }  
  28. }  

WCF集合元素自定义集合DocumentList则实现了IList接口:

 

  1. //which attribute should be applied here?  
  2. public class DocumentList:IList 
  3. {  
  4. private IList m_list = null;  
  5. public DocumentList()  
  6. {  
  7. m_list = new List();  
  8. }  
  9. #region IList Members  
  10. public int IndexOf(Document item)  
  11. {  
  12. return m_list.IndexOf(item);  
  13. }  
  14. public void Insert(int index, Document item)  
  15. {  
  16. m_list.Insert(index,item);  
  17. }  
  18. public void RemoveAt(int index)  
  19. {  
  20. m_list.RemoveAt(index);  
  21. }  
  22. public Document this[int index]  
  23. {  
  24. get  
  25. {  
  26. return m_list[index];  
  27. }  
  28. set  
  29. {  
  30. m_list[index] = value;  
  31. }  
  32. }  
  33. #endregion  
  34. #region ICollection Members  
  35. public void Add(Document item)  
  36. {  
  37. m_list.Add(item);  
  38. }  
  39. public void Clear()  
  40. {  
  41. m_list.Clear();  
  42. }  
  43. public bool Contains(Document item)  
  44. {  
  45. return m_list.Contains(item);  
  46. }  
  47. public void CopyTo(Document[] array, int arrayIndex)  
  48. {  
  49. m_list.CopyTo(array,arrayIndex);  
  50. }  
  51. public int Count  
  52. {  
  53. get { return m_list.Count; }  
  54. }  
  55. public bool IsReadOnly  
  56. {  
  57. get { return m_list.IsReadOnly; }  
  58. }  
  59. public bool Remove(Document item)  
  60. {  
  61. return m_list.Remove(item);  
  62. }  
  63. #endregion  
  64. #region IEnumerable Members  
  65. public IEnumerator GetEnumerator()  
  66. {  
  67. return m_list.GetEnumerator();  
  68. }  
  69. #endregion  
  70. #region IEnumerable Members  
  71. IEnumerator IEnumerable.GetEnumerator()  
  72. {  
  73. return ((IEnumerable)m_list).GetEnumerator();  
  74. }  
  75. #endregion  

注意,对于自定义集合DocumentList而言,我们不能应用[DataContract]特性,否则会在服务操作中无法返回正确的DocumentList对象。例如如下的服务操作定义,实际上无法获得正确的DocumentList值:

  1. [OperationContract]  
  2. [FaultContract(typeof
    (DirectoryNotFoundException))]  
  3. DocumentList FetchDocuments
    (string homeDir); 

我们应该为DocumentList施加[CollectionDataContract]或者[Serializable],建议采用前者。因为对于自定义集合而言,如果是泛型集合,还可以利用Name属性制定导出元数据生成的类型名。不过,对于本例的集合而言,由于没有泛型参数,则无所谓了。为了在导出元数据时识别集合的元素Document类型,当然,还需要施加KnowTypeAttribute,最后的定义修改如下:
 

  1. [KnownType(typeof(Document))]  
  2. [CollectionDataContract]   
  3. [Serializable]  
  4. public class DocumentList:
    IList 
  5. {} 

此时,客户端应用程序可以直接使用数据契约,仍然能够识别WCF集合元素。

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...