客户端代理的作用

客户端代理的作用
•在对象里做了一个标记
–“__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”

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类是一个抽象类,它必须被继承才能过使用

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中的一种就可以了。

转载于:https://www.cnblogs.com/timy/archive/2008/04/27/1173088.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值