1 [Serializable] 2 public class Employee : IComparable<Employee> 3 { 4 public string Name; 5 6 public int Age; 7 8 #region IComparable<Employee> Members 9 10 public int CompareTo(Employee other) 11 { 12 return this.Name.CompareTo(other.Name); 13 } 14 15 #endregion 16 } 17 18 [Serializable] 19 public class Company 20 { 21 public string Name; 22 23 public Employee[] Employees; 24 } 接著我們定義一個(gè)Web Services方法Sort,該方法的作用是拿到公司姓名和一個(gè)Employee數(shù)組作為參數(shù),將Employee按照姓名排序之后,再組成一個(gè)Company對(duì)象輸出。代碼如下:
Sort方法
1 [WebService(Namespace = "http://tempuri.org/")] 2 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 3 public class ComplexTypeWS : System.Web.Services.WebService { 4 5 [WebMethod] 6 public Company Sort(string companyName, Employee[] employees) 7 { 8 Array.Sort<Employee>(employees); 9 10 Company company = new Company(); 11 company.Name = companyName; 12 company.Employees = employees; 13 return company; 14 } 15 } 然后就是Html了。在頁(yè)面最上方(id為employees的div)會(huì)顯示內(nèi)存中目前所有的Employee,之后是向內(nèi)存中添加Employee的輸入框,接著是填寫(xiě)公司名的文本框和排序按鈕,最后則是經(jīng)過(guò)了Web Services排序后的結(jié)果顯示區(qū)域(id為sortedDisplay的div):