一、定义抽象类及子类,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();
        }
    }

 

相关文章:

  • 2021-12-17
  • 2021-12-12
  • 2021-07-01
  • 2022-12-23
  • 2022-12-23
  • 2021-07-09
  • 2021-09-02
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-04-27
  • 2022-12-23
  • 2022-12-23
  • 2022-01-11
  • 2022-12-23
  • 2022-01-25
相关资源
相似解决方案