services多態的使用方法

時間:2024-09-26 23:12:32 Web Services 我要投稿
  • 相關推薦

services多態的使用方法

  在Web Services方法中,往往使用的都是一個具體類型的參數,這個參數一般就是一個數據對象。ASP.NET Web Services通過聲明XmlIncludeAttribute可以實現Web Services方法中運用多態。

  XmlIncludeAttribute允許XmlSerializer在序列化火反序列化對象時識別類型。當應用XmlIncludeAttribute時,需指定派生類的Type。XmlSerializer序列化同時包含基類和派生類的對象之后,它就可以識別兩種對象類型。

  首先定義基類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需要添加對命名空間System.Xml.Serialization的引用:

  [WebMethod] [XmlInclude(typeof(Car))] public void AddVehicle(Vehicle vehicle) { }

  查看生成的wsdl,wsdl利用extension的base屬性來描述Car繼承Vechicle。

  查看引用Web Services生成的Reference.cs文件,Vehicle類會有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

  客戶端測試代碼:

  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方法可以查看傳過來的參數:

  Web Services可以支持多態,不過僅僅限制在可以直接引用Web Services的時候有生成可序列化的代碼時能夠使用,要在其他的客戶端使用還是得費一番周折。

  以上就是本文的全部內容,希望對大家的學習有所幫助。

【services多態的使用方法】相關文章:

早教機的使用方法07-27

早教機使用方法02-27

武術器械使用方法09-03

早教機藍牙的使用方法09-16

西餐餐巾的使用方法09-17

護發膜使用方法10-26

日語常用敬語使用方法01-21

閃卡早教使用方法12-09

BB霜的正確使用方法09-25

CAD陣列命令的使用方法06-21

亚洲制服丝袜二区欧美精品,亚洲精品无码视频乱码,日韩av无码一区二区,国产人妖视频一区二区
在线观看AV网站麻豆 | 亚洲国产在人线午夜播放 | 中文字幕欧美在线不卡二区 | 中文字幕乱码亚洲中文在线 | 婷婷中文字幕精品 | 亚洲国产午夜站香蕉 |