•在对象里做了一个标记
–“__type” = “ComplexType.Color”
•服务器端根据标记选择反序列化的目标类型
•可出现“多态”效果
aspx
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="EmployeeService.asmx" InlineScript="true" />
</Services>
</asp:ScriptManager>
<div>Years:<input type="text" id="txtYears" /></div>
<div>
Status:
<select id="comboStatus" style="width:150px;">
<option value="ComplexType.Intern">Intern</option>
<option value="ComplexType.Vendor">Vendor</option>
<option value="ComplexType.FulltimeEmployee">FTE</option>
</select>
</div>
<input type="button" onclick="calculateSalary()" value="Calculate!" />
<br />
<b>Result:</b>
<div id="result"></div>
<script language="javascript" type="text/javascript">
function calculateSalary()
{
var emp = new Object();
emp.__type = $get("comboStatus").value;
emp.Years = parseInt($get("txtYears").value, 10);
EmployeeService.CalculateSalary(emp, onSucceeded);
}
function onSucceeded(result)
{
$get("result").innerHTML = result;
}
</script>
</form>
Employees.cs
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;
namespace ComplexType
{
public abstract class Employee
{
private int _Years;
public int Years
{
get
{
return this._Years;
}
set
{
this._Years = value;
}
}
public string RealStatus
{
get
{
return this.GetType().Name;
}
}
public abstract int CalculateSalary();
}
public class Intern : Employee
{
public override int CalculateSalary()
{
return 2000;
}
}
public class Vendor : Employee
{
public override int CalculateSalary()
{
return 5000 + 1000 * (Years - 1);
}
}
public class FulltimeEmployee : Employee
{
public override int CalculateSalary()
{
return 15000 + 2000 * (Years - 1);
}
}
}
EmployeeService.asmx
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using ComplexType;
/// <summary>
/// Summary description for EmployeeService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class EmployeeService : System.Web.Services.WebService
{
[WebMethod]
[GenerateScriptType(typeof(Intern))]
[GenerateScriptType(typeof(Vendor))]
[GenerateScriptType(typeof(FulltimeEmployee))]
public string CalculateSalary(Employee employee)
{
return "I'm " + employee.RealStatus +
", my salary is " + employee.CalculateSalary() + ".";
}
}