客户端代理的作用
客户端代理的作用
•在对象里做了一个标记
–“__type” = “ComplexType.Color”
•服务器端根据标记选择反序列化的目标类型
•可出现“多态”效果
aspx
Employees.cs
EmployeeService.asmx
•在对象里做了一个标记
–“__type” = “ComplexType.Color”
•服务器端根据标记选择反序列化的目标类型
•可出现“多态”效果
aspx
<form id="form1" runat="server">
<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>
定义一个emp的Object对象,指定“__type”(何种类型对象,是要转换成相应的服务器端的类型的)的值,两个下划线“_”加上“type”<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;
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);
}
}
}
这里的Employee类是一个抽象类,它必须被继承才能过使用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
<%@ WebService Language="C#" Class="EmployeeService" %>
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() + ".";
}
}
这里的CalculateSalary方法指定要生成的参数类型不是Employee而是继承Employee的类的Intern、Vendor、FulltimeEmployee的子类,是因为Employee是抽象类,在传入参数时只要传入Intern、Vendor、FulltimeEmployee中的一种就可以了。
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() + ".";
}
}