Web Services使用多態(tài)的方法
在Web Services方法中,往往使用的都是一個(gè)具體類型的參數(shù),這個(gè)參數(shù)一般就是一個(gè)數(shù)據(jù)對(duì)象。ASP.NET Web Services通過(guò)聲明XmlIncludeAttribute可以實(shí)現(xiàn)Web Services方法中運(yùn)用多態(tài)。
XmlIncludeAttribute允許XmlSerializer在序列化火反序列化對(duì)象時(shí)識(shí)別類型。當(dāng)應(yīng)用XmlIncludeAttribute時(shí),需指定派生類的Type。XmlSerializer序列化同時(shí)包含基類和派生類的對(duì)象之后,它就可以識(shí)別兩種對(duì)象類型。
首先定義基類Vehicle和派生類Car:
public abstract class Vehicle { public string LicenseNumber{get;set;} public DateTime MakeTime { get; set; } } public class Car : Vehicle { public int DoorNum { get; set; } }
定義AddVehicle的Web Method,聲明XmlInclude需要添加對(duì)命名空間System.Xml.Serialization的引用:
[WebMethod] [XmlInclude(typeof(Car))] public void AddVehicle(Vehicle vehicle) { }
查看生成的wsdl,wsdl利用extension的base屬性來(lái)描述Car繼承Vechicle。
查看引用Web Services生成的Reference.cs文件,Vehicle類會(huì)有XmlIncludeAttribute的聲明:
[System.Xml.Serialization.XmlIncludeAttribute(typeof(Car))] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.1")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/")] public abstract partial class Vehicle : object
客戶端測(cè)試代碼:
static void Main(string[] args) { localhost.WebService1SoapClient c = new localhost.WebService1SoapClient(); localhost.Car car = new localhost.Car() { LicenseNumber="0001", MakeTime=DateTime.Now, DoorNum=2 c.AddVehicle(car); }
在Web Services的AddVehicle方法可以查看傳過(guò)來(lái)的參數(shù):
Web Services可以支持多態(tài),不過(guò)僅僅限制在可以直接引用Web Services的時(shí)候有生成可序列化的代碼時(shí)能夠使用,要在其他的客戶端使用還是得費(fèi)一番周折。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。
相關(guān)文章
asp.net 從客戶端中檢測(cè)到有潛在危險(xiǎn)的 Request.Form 值錯(cuò)誤解
asp.net程序運(yùn)行時(shí)出現(xiàn)以下錯(cuò)誤: “/news”應(yīng)用程序中的服務(wù)器錯(cuò)誤。2009-05-05ASP.NET簡(jiǎn)化編輯界面解決思路及實(shí)現(xiàn)代碼
簡(jiǎn)化用戶操作界面,添加功能一般沒(méi)法簡(jiǎn)化,但是如果是在GridView做顯示,編輯,更新與刪除,會(huì)讓用戶在編輯,需要點(diǎn)擊編輯銨鈕,再進(jìn)行編輯,或是取消編輯,下面是改進(jìn)的方法做了演示動(dòng)畫,感興趣的朋友可以了解下2013-01-01ASP.NET 返回隨機(jī)數(shù)實(shí)現(xiàn)代碼
ASP.NET返回隨機(jī)數(shù),需要的朋友可以參考下。2009-11-11asp.net TextBox控件設(shè)置ReadOnly后,不能回傳。
當(dāng)把一個(gè)TextBox控件ReadOnly屬性設(shè)置為True后,這個(gè)控件就不回傳了。2009-05-05ASP.NET下對(duì)cookies的操作實(shí)現(xiàn)代碼
ASP.NET下對(duì)cookies的操作實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-10-10大型門戶網(wǎng)站實(shí)現(xiàn)的十四大技術(shù)小結(jié)
參考下大型門戶網(wǎng)站的技術(shù),大家可以盡量的備份好服務(wù)器。2010-10-10