最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

Web Services使用多态的方法

来源:懂视网 责编:小采 时间:2020-11-27 22:38:11
文档

Web Services使用多态的方法

Web Services使用多态的方法:在Web Services方法中,往往使用的都是一个具体类型的参数,这个参数一般就是一个数据对象。ASP.NET Web Services通过声明XmlIncludeAttribute可以实现Web Services方法中运用多态。 XmlIncludeAttribute允许XmlSerializer在序
推荐度:
导读Web Services使用多态的方法:在Web Services方法中,往往使用的都是一个具体类型的参数,这个参数一般就是一个数据对象。ASP.NET Web Services通过声明XmlIncludeAttribute可以实现Web Services方法中运用多态。 XmlIncludeAttribute允许XmlSerializer在序

在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使用多态的方法

Web Services使用多态的方法:在Web Services方法中,往往使用的都是一个具体类型的参数,这个参数一般就是一个数据对象。ASP.NET Web Services通过声明XmlIncludeAttribute可以实现Web Services方法中运用多态。 XmlIncludeAttribute允许XmlSerializer在序
推荐度:
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top