WCF數據契約不同名稱特點介紹
在WCF開發插件中,有很多比較重要的知識內容值得我們在實踐中去不斷的積累經驗來對此進行深入研究,比如今天為大家介紹的WCF數據契約,就是一個比較重要的內容。接下來就讓沒一起來看看它的實質內容吧。#t#
缺省情況下,WCF框架對集合類型是內建支持的,也就說你不需要應用任何屬性,就可以將集合應用在數據契約(協定)中,但前提是集合中的元素必須是應用了DataContractAttribute屬性或者是可序列化的類型。這時,數據契約(協定)名稱和命名空間就依賴集合中包含的元素的類型的名稱和命名空間了,它們不受集合類型本身的名稱和命名空間的影響。
缺省集合類型WCF數據契約(協定)的格式是(不包括“+”):
列表集合:名稱:ArrayOf+集合中包含的元素類型
循環元素名稱:集合中包含的元素類型
字典集合:名稱:ArrayOfKeyValueOf+集合中Key的類型+集合中包含的對象類型
循環元素名稱:KeyValueOf+集合中Key的類型+集合中包含的對象類型
例如:
MyCollection1 : IList< int>{…}的數據契約名稱就是:ArrayOfint
MyCollection2 : ICollection< int>{…}的WCF數據契約名稱就是:ArrayOfint
MyDictionary1 : Dictionary< int, int>{…}的數據契約名稱就是:ArrayOfKeyValueOfintint
MyCollection3 : ArrayList{…}的數據契約名稱就是:ArrayOfanyType
MyDictionary2 : Dictionary< int, object>{…}的數據契約名稱就是:ArrayOfKeyValueOfintanyType
注意:如果是object的話,使用的是anyType,因為在Schema中所有類型的基類是anyType.
如果集合是應用于某個WCF數據契約類型中時,那么它的名稱將是字段名稱,如下面Customer的定義以及序列化后的表示:
- [DataContract]
- public class Customer
- {
- [DataMember]
- public List< string> addresses = new List< string> {"Beijing","ShangHai" };
- [DataMember]
- public Dictionary< int, object> telephones = new Dictionary< int, object> {
- { 1, "010-82371234" },
- { 2, "021-56781234" } };
- }
- < Customer xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://schemas.datacontract.org/2004/07/WCFTestSerializer">
- < addresses xmlns: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:Value xmlns: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:Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema"
i:type="d4p1:string">021-56781234< /d2p1:Value>- < /d2p1:KeyValueOfintanyType>
- < /telephones>
- < /Customer>
以上就是我們為大家介紹的WCF數據契約相關內容。