码迷,mamicode.com
首页 > Web开发 > 详细

使用XmlInclude解决WebService调用时无法识别子类的异常

时间:2014-11-29 21:41:07      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   使用   sp   strong   

一、定义抽象类及子类,WebMethod实际返回子类参数

  //使用XmlInclude解决WebService调用时无法识别子类的异常
    [System.Xml.Serialization.XmlInclude(typeof(WageEmploeyee)), System.Xml.Serialization.XmlInclude(typeof(Boss))]
    public abstract class EmployeeData
    {
        //Required by XmlSerializer
        public EmployeeData() { }

        public string Name { get; set; }

        public string SSN { get; set; }

        public abstract double ComputerPay();
    }

    public class WageEmploeyee : EmployeeData
    {
        public double Wage { get; set; }

        public double Hours { get; set; }

        public override double ComputerPay()
        {
            return this.Wage * this.Hours;
        }
    }

    public class Boss : EmployeeData
    {
        public double Salary { get; set; }

        public override double ComputerPay()
        {
            return this.Salary;
        }
    }

 

2、WebMethod方法(根据传入的参数实例化不同的子类)

  public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public EmployeeData GetEmployee(int id)
        {
            if (id == 1)
            {
                return new Boss();
            }

            return new WageEmploeyee();
        }
    }

 

使用XmlInclude解决WebService调用时无法识别子类的异常

标签:style   blog   io   ar   color   os   使用   sp   strong   

原文地址:http://www.cnblogs.com/gossip/p/4131987.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!