复杂参数的基本使用方式

复杂参数的基本使用方式
•作为参数的类型一定要有默认的构造函数
–编译器自动生成默认的构造函数
–手动编写默认的构造函数
•客户端设置类型共有的域变量及属性
–设置的属性必须有Setter方法
•使用JSON进行传输格式
•自动进行序列化与反序列化

aspx
    <form id="form1" runat="server">
        
<asp:ScriptManager ID="ScriptManager1" runat="server">
            
<Services>
                
<asp:ServiceReference Path="ColorService.asmx" InlineScript="true" />
            
</Services>
        
</asp:ScriptManager>
        
        
<input type="button" value="Reverse Color" onclick="ReverseColor()" />
        
        
<script language="javascript" type="text/javascript">
            function ReverseColor()
            {
                var color 
= {"Red" : 50"Green" : 100"Blue" : 200};
                ColorService.Reverse(color, onSucceeded);
            }
            
            function onSucceeded(result)
            {
                alert(String.format(
                    
"Red: {0}\nGreen: {1}\nBlue: {2}",
                    result.Red,
                    result.Green,
                    result.Blue));
            }            
        
</script>
    
</form>

ColorService.asmx
<%@ WebService Language="C#" Class="ColorService" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using ComplexType;

[WebService(Namespace 
= "http://tempuri.org/")]
[WebServiceBinding(ConformsTo 
= WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class ColorService  : System.Web.Services.WebService
{
    [WebMethod]
    [GenerateScriptType(
typeof(Color))]
    
public Color Reverse(Color color)
    {
        
return new Color(
            (
byte)(255 - color.Red),
            (
byte)(255 - color.Green),
            (
byte)(255 - color.Blue));
    }
    
}

Color.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
{
    
/// <summary>
    
/// Summary description for Color
    
/// </summary>
    public class Color
    {
        
public Color() { }

        
public Color(byte red, byte green, byte blue)
        {
            
this.Red = red;
            
this.Green = green;
            
this.Blue = blue;
        }

        
public byte Red;

        
public byte Green;

        
public byte Blue;
    }
}
posted on 2008-04-27 14:20  一粒沙  阅读(312)  评论(0编辑  收藏  举报