標籤: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調用時無法識別子類的異常