WCF數(shù)據(jù)契約不同名稱特點(diǎn)介紹
在WCF開發(fā)插件中,有很多比較重要的知識(shí)內(nèi)容值得我們?cè)趯?shí)踐中去不斷的積累經(jīng)驗(yàn)來對(duì)此進(jìn)行深入研究,比如今天為大家介紹的WCF數(shù)據(jù)契約,就是一個(gè)比較重要的內(nèi)容。接下來就讓沒一起來看看它的實(shí)質(zhì)內(nèi)容吧。#t#
缺省情況下,WCF框架對(duì)集合類型是內(nèi)建支持的,也就說你不需要應(yīng)用任何屬性,就可以將集合應(yīng)用在數(shù)據(jù)契約(協(xié)定)中,但前提是集合中的元素必須是應(yīng)用了DataContractAttribute屬性或者是可序列化的類型。這時(shí),數(shù)據(jù)契約(協(xié)定)名稱和命名空間就依賴集合中包含的元素的類型的名稱和命名空間了,它們不受集合類型本身的名稱和命名空間的影響。
缺省集合類型WCF數(shù)據(jù)契約(協(xié)定)的格式是(不包括“+”):
列表集合:名稱:ArrayOf+集合中包含的元素類型
循環(huán)元素名稱:集合中包含的元素類型
字典集合:名稱:ArrayOfKeyValueOf+集合中Key的類型+集合中包含的對(duì)象類型
循環(huán)元素名稱:KeyValueOf+集合中Key的類型+集合中包含的對(duì)象類型
例如:
MyCollection1 : IList< int>{…}的數(shù)據(jù)契約名稱就是:ArrayOfint
MyCollection2 : ICollection< int>{…}的WCF數(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.
如果集合是應(yīng)用于某個(gè)WCF數(shù)據(jù)契約類型中時(shí),那么它的名稱將是字段名稱,如下面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>
以上就是我們?yōu)榇蠹医榻B的WCF數(shù)據(jù)契約相關(guān)內(nèi)容。