•公有属性或公有Field会被释放和接受
•容器对象
–实现IList接口的对象
–实现IDictionary接口的对象
•Key必须是String
aspx
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="ComplexType.asmx" />
</Services>
</asp:ScriptManager>
<input type="button" value="Double Salary" onclick="doubleSalary()" />
<input type="button" value="Reverse" onclick="reverse([1, 2, 3, 4, 5])" />
<input type="button" value="Get Employees" onclick="getEmployees()" />
<script language="javascript" type="text/javascript">
function doubleSalary()
{
var employee = new Object();
employee.FirstName = "Jeffrey";
employee.LastName = "Zhao";
employee.Salary = 1000;
ComplexType.DoubleSalary(employee, doubleSalarySucceeded);
}
function doubleSalarySucceeded(result)
{
var message = String.format(
"First Name: {0}\nLast Name: {1}\nFull Name: {2}\nSalary: {3}",
result.FirstName,
result.LastName,
result.FullName,
result.Salary);
alert(message);
}
function reverse(array)
{
ComplexType.Reverse(array, function(result){alert(result);});
}
function getEmployees()
{
ComplexType.GetEmployees(getEmployeesSucceeded);
}
function getEmployeesSucceeded(result)
{
for (var name in result)
{
alert(name + ": " + result[name].Salary)
}
}
</script>
</form>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="ComplexType.asmx" />
</Services>
</asp:ScriptManager>
<input type="button" value="Double Salary" onclick="doubleSalary()" />
<input type="button" value="Reverse" onclick="reverse([1, 2, 3, 4, 5])" />
<input type="button" value="Get Employees" onclick="getEmployees()" />
<script language="javascript" type="text/javascript">
function doubleSalary()
{
var employee = new Object();
employee.FirstName = "Jeffrey";
employee.LastName = "Zhao";
employee.Salary = 1000;
ComplexType.DoubleSalary(employee, doubleSalarySucceeded);
}
function doubleSalarySucceeded(result)
{
var message = String.format(
"First Name: {0}\nLast Name: {1}\nFull Name: {2}\nSalary: {3}",
result.FirstName,
result.LastName,
result.FullName,
result.Salary);
alert(message);
}
function reverse(array)
{
ComplexType.Reverse(array, function(result){alert(result);});
}
function getEmployees()
{
ComplexType.GetEmployees(getEmployeesSucceeded);
}
function getEmployeesSucceeded(result)
{
for (var name in result)
{
alert(name + ": " + result[name].Salary)
}
}
</script>
</form>
cs
protected void Page_Load(object sender, EventArgs e)
{
}
{
}
ComplexType.asmx
<%@ WebService Language="C#" Class="ComplexType" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class ComplexType : System.Web.Services.WebService {
[WebMethod]
public Employee DoubleSalary(Employee employee)
{
employee.Salary *= 2;
return employee;
}
[WebMethod]
public List<int> Reverse(List<int> list)
{
list.Reverse();
return list;
}
[WebMethod]
public IDictionary<string, Employee> GetEmployees()
{
Dictionary<string, Employee> result = new Dictionary<string, Employee>();
Employee emp1 = new Employee();
emp1.FirstName = "Jeffrey";
emp1.LastName = "Zhao";
emp1.Salary = 1000;
result[emp1.FullName] = emp1;
Employee emp2 = new Employee();
emp2.FirstName = "Tom";
emp2.LastName = "Chen";
emp2.Salary = 2000;
result[emp2.FullName] = emp2;
return result;
}
}
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class ComplexType : System.Web.Services.WebService {
[WebMethod]
public Employee DoubleSalary(Employee employee)
{
employee.Salary *= 2;
return employee;
}
[WebMethod]
public List<int> Reverse(List<int> list)
{
list.Reverse();
return list;
}
[WebMethod]
public IDictionary<string, Employee> GetEmployees()
{
Dictionary<string, Employee> result = new Dictionary<string, Employee>();
Employee emp1 = new Employee();
emp1.FirstName = "Jeffrey";
emp1.LastName = "Zhao";
emp1.Salary = 1000;
result[emp1.FullName] = emp1;
Employee emp2 = new Employee();
emp2.FirstName = "Tom";
emp2.LastName = "Chen";
emp2.Salary = 2000;
result[emp2.FullName] = emp2;
return result;
}
}
Employee.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for Employee
/// </summary>
public class Employee
{
public string FirstName;
public string LastName;
public int Salary;
public string FullName
{
get
{
return this.FirstName + " " + this.LastName;
}
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for Employee
/// </summary>
public class Employee
{
public string FirstName;
public string LastName;
public int Salary;
public string FullName
{
get
{
return this.FirstName + " " + this.LastName;
}
}
}