zoukankan      html  css  js  c++  java
  • 使用XmlInclude解决WebService调用时无法识别子类的异常

    一、定义抽象类及子类,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();
            }
        }
  • 相关阅读:
    博弈论基础与习题(未完)
    三视图求最多方块数
    二维前缀和应用
    卡特兰数
    UVa 11806 Cheerleaders(容斥定理)
    逃出升天
    求排列的逆序数
    求2进制下1的个数
    字符串哈希基础与应用
    网络流基础与应用
  • 原文地址:https://www.cnblogs.com/gossip/p/4131987.html
Copyright © 2011-2022 走看看