自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

WCF集合元素基本概念詳解

開發(fā) 開發(fā)工具
WCF集合元素基本概念對于初學(xué)者來說是必須要掌握的,充分掌握這篇文章介紹的內(nèi)容,可以幫助我們提高對WCF的理解程度。

WCF開發(fā)框架中有很多集合元素,在這里我們會為大家詳細介紹這些WCF集合元素的定義方法,和相關(guān)使用技巧,希望對大家有所幫助。#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則實現(xiàn)了IList<Document>接口:

 

  1. //which attribute should be applied here?  
  2. public class DocumentList:IList<Document> 
  3. {  
  4. private IList<Document> m_list = null;  
  5. public DocumentList()  
  6. {  
  7. m_list = new List<Document>();  
  8. }  
  9. #region IList<Document> 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<Document> 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<Document> Members  
  65. public IEnumerator<Document> 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而言,我們不能應(yīng)用[DataContract]特性,否則會在服務(wù)操作中無法返回正確的DocumentList對象。例如如下的服務(wù)操作定義,實際上無法獲得正確的DocumentList值:

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

我們應(yīng)該為DocumentList施加[CollectionDataContract]或者[Serializable],建議采用前者。因為對于自定義集合而言,如果是泛型集合,還可以利用Name屬性制定導(dǎo)出元數(shù)據(jù)生成的類型名。不過,對于本例的集合而言,由于沒有泛型參數(shù),則無所謂了。為了在導(dǎo)出元數(shù)據(jù)時識別集合的元素Document類型,當(dāng)然,還需要施加KnowTypeAttribute,最后的定義修改如下:
 

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

此時,客戶端應(yīng)用程序可以直接使用數(shù)據(jù)契約,仍然能夠識別WCF集合元素。

責(zé)任編輯:曹凱 來源: 博客園
相關(guān)推薦

2010-02-23 16:32:29

WCF服務(wù)

2010-02-24 17:17:04

WCF宿主環(huán)境

2009-12-21 10:27:52

WCF基本概念

2010-03-01 18:04:35

WCF配置綁定

2010-03-01 14:50:30

WCF行為類型

2010-03-01 16:25:07

WCF體系架構(gòu)

2010-03-02 13:14:38

WCF MSMQ隊列

2010-03-01 16:41:04

WCF數(shù)據(jù)表

2010-03-02 16:22:31

WCF狀態(tài)應(yīng)用

2009-12-21 16:52:02

WCF序列化

2009-12-22 10:16:54

WCF服務(wù)狀態(tài)

2009-12-21 14:37:14

2010-02-25 14:46:31

2010-03-02 11:10:43

WCF標準終結(jié)點

2010-07-12 09:43:38

Symbian開發(fā)

2010-03-01 17:57:11

WCF緩存機制

2009-12-29 18:29:09

Silverlight

2010-02-02 10:33:22

C++引用

2010-02-02 15:30:05

C++ include

2011-03-28 11:05:17

ODBC
點贊
收藏

51CTO技術(shù)棧公眾號