无为清净楼资源网 Design By www.qnjia.com
在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的时候有生成可序列化的代码时能够使用,要在其他的客户端使用还是得费一番周折。
以上就是本文的全部内容,希望对大家的学习有所帮助。
标签:
Web,Services,多态
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月27日
2024年09月27日
- 梁静茹.2007-崇拜【相信音乐】【WAV+CUE】
- 金佩姗.1997-喜新恋旧【现代派】【WAV+CUE】
- 梁静茹.2007-崇拜【相信音乐】【WAV+CUE】
- 陈晓东.1997-在乎你感受(EP)【宝丽金】【WAV+CUE】
- 纯音入心系列纯音乐《云水禅心 - 佛曲古筝礼赞1-2》1CD[MP3][190MB]
- 纯音入心系列纯音乐《精心传神-古筝》1CD[MP3][363.1MB]
- 纯音入心系列纯音乐《琴瑟古风之古筝精选曲》1CD[MP3][354.1MB]
- 陈晓东.1997-在乎你感受(EP)【宝丽金】【WAV+CUE】
- 黄凯芹.1990-给你留念(2024环球MQA-UHQCD限量版)【环球】【WAV+CUE】
- 张学友.1999-有个人【宝丽金】【WAV+CUE】
- 纯音入心系列纯音乐《古筝佛曲》1CD[MP3][411.1MB]
- 纯音入心系列纯音乐《古筝世界名曲》1CD[MP3][38.9MB]
- 纯音入心系列纯音乐《古筝新奏-菩提妙音》1CD[MP3][112.3MB]
- 陈思安.1993-怀念福建金曲5CD【歌丽美】【WAV+CUE】
- 张学友.1999-有个人【宝丽金】【WAV+CUE】