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)文章
MVC4制作網(wǎng)站教程第二章 部分用戶功能實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了MVC4制作網(wǎng)站教程,部分用戶功能實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
asp.net 判斷數(shù)組是否存在某個(gè)值的方法
asp.net 判斷數(shù)組是否存在某個(gè)值的兩種方法, 需要的朋友可以參考下。2010-07-07
asp.net使用jquery實(shí)現(xiàn)搜索框默認(rèn)提示功能
這篇文章主要介紹了asp.net使用jquery實(shí)現(xiàn)搜索框默認(rèn)提示功能,大家參考使用吧2014-01-01
asp.net(C#)使用QRCode生成圖片中心加Logo或圖像的二維碼實(shí)例
這篇文章主要介紹了asp.net(C#)使用QRCode生成圖片中心加Logo或圖像的二維碼,結(jié)合實(shí)例形式詳細(xì)分析了asp.net基于QRCode生成二維碼的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-06-06
ASP.NET.4.5.1+MVC5.0設(shè)置系統(tǒng)角色與權(quán)限(二)
這篇文章主要介紹了使用ASP.NET.4.5.1+MVC5.0構(gòu)建項(xiàng)目中設(shè)置系統(tǒng)角色的全部過(guò)程,十分的詳細(xì),附上全部源碼,推薦給想學(xué)習(xí).net+mvc的小伙伴們2015-01-01
ASP.NET實(shí)現(xiàn)電影票信息的增刪查改功能
這篇文章主要介紹了ASP.NET實(shí)現(xiàn)電影票信息的增刪查改功能的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12
ASP.NET?Core?MVC中Form?Tag?Helpers用法介紹
這篇文章介紹了ASP.NET?Core?MVC中Form?Tag?Helpers的用法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02

