zoukankan      html  css  js  c++  java
  • 客户端代理的作用

    客户端代理的作用
    •在对象里做了一个标记
    –“__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="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中的一种就可以了。
  • 相关阅读:
    EasyRadius 动态域名DDNS设置工具,支持WayOS三代,完美解决近段时间3322和每步不稳定问题
    爱快路由计费系统easyradius隆重发布,支持V2版本,欢迎大家测试使用
    easyradius隆重发布ROS API计费接口,支持ROS 3.3以上版本,实现简单快捷的ROS宽带计费系统云端版
    easyradius通讯接口 V4全新升级,显示同步失败原因,方便用户寻找故障
    上网爱快?EasyRadius FOR 爱快V2接口测试版正式推出,欢迎广大爱迷们测试噢
    让小区运营再智能一点,EasyRadius正式向WayOs用户提供到期弹出式提示充值页面
    Easyradius对接WayOs维盟小区版XQ教程
    由于PADT伪造攻击带来的大面积掉线原因分析
    WiFidog 广告路由可修改功能更加智能化的几点看法
    TFTP 1.68智能刷机全能版发布,TTL线在CFE模式解决BCM5357如斐讯FIR302B等产品变砖问题
  • 原文地址:https://www.cnblogs.com/timy/p/1173088.html
Copyright © 2011-2022 走看看