關(guān)于WCF集合類型中數(shù)據(jù)契約詳細(xì)介紹
經(jīng)過長(zhǎng)時(shí)間學(xué)習(xí)WCF,于是和大家分享一下關(guān)于集合的數(shù)據(jù)契約(協(xié)定)缺省名稱,看完本文你肯定有不少收獲,希望本文能教會(huì)你更多東西。
#T#缺省情況下,WCF框架對(duì)集合類型是內(nèi)建支持的,也就說你不需要應(yīng)用任何屬性,就可以將集合應(yīng)用在數(shù)據(jù)契約(協(xié)定)中,但前提是集合中的元素必須是應(yīng)用了屬性或者是可序列化的類型。這時(shí),數(shù)據(jù)契約(協(xié)定)名稱和命名空間就依賴WCF集合類型中包含的元素的類型的名稱和命名空間了,它們不受WCF集合類型本身的名稱和命名空間的影響。
WCF缺省集合類型數(shù)據(jù)契約(協(xié)定)的格式是(不包括“+”):
◆列表集合:名稱:ArrayOf+集合中包含的元素類型
◆循環(huán)元素名稱:集合中包含的元素類型
◆字典集合:名稱:ArrayOfKeyValueOf+集合中Key的類型+集合中包含的對(duì)象類型
◆循環(huán)元素名稱:KeyValueOf+集合中Key的類型+集合中包含的對(duì)象類型
例如:
- MyCollection1:IList<int>{…}的數(shù)據(jù)契約名稱就是:ArrayOfint
- MyCollection2:ICollection<int>{…}的數(shù)據(jù)契約名稱就是:ArrayOfint
- MyDictionary1:Dictionary<int,int>{…}的數(shù)據(jù)契約名稱就是:ArrayOfKeyValueOfintint
- MyCollection3:ArrayList{…}的數(shù)據(jù)契約名稱就是:ArrayOfanyType
- MyDictionary2:Dictionary<int,object>{…}的數(shù)據(jù)契約名稱就是:ArrayOfKeyValueOfintanyType
注意:如果是object的話,使用的是anyType,因?yàn)樵赟chema中所有類型的基類是anyType.
如果WCF集合類型是應(yīng)用于某個(gè)數(shù)據(jù)契約類型中時(shí),那么它的名稱將是字段名稱,如下面Customer的定義以及序列化后的表示:
- [DataContract]
- publicclassCustomer
- {
- [DataMember]
- publicList<string>addresses=newList<string>{"Beijing","ShangHai"};
- [DataMember]
- publicDictionary<int,object>telephones=newDictionary<int,object>{
- {1,"010-82371234"},
- {2,"021-56781234"}};
- }
- <Customerxmlns:iCustomerxmlns:i="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://schemas.datacontract.org/2004/07/WCFTestSerializer">
- <addressesxmlns:d2p1addressesxmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
- <d2p1:string>Beijing</d2p1:string>
- <d2p1:string>ShangHai</d2p1:string>
- </addresses>
- <telephones
- xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
- <d2p1:KeyValueOfintanyType>
- <d2p1:Key>1</d2p1:Key>
- <d2p1:Valuexmlns:d4p1d2p1:Valuexmlns:d4p1="http://www.w3.org/2001/XMLSchema"i:type="d4p1:string">010-82371234</d2p1:Value>
- </d2p1:KeyValueOfintanyType>
- <d2p1:KeyValueOfintanyType>
- <d2p1:Key>2</d2p1:Key>
- <d2p1:Valuexmlns:d4p1d2p1:Valuexmlns:d4p1="http://www.w3.org/2001/XMLSchema"i:type="d4p1:string">021-56781234</d2p1:Value>
- </d2p1:KeyValueOfintanyType>
- </telephones>
- </Customer>