五、复杂数据类型的通讯
Remoting支持传送数组、List、HashTable、Dictionary等多种复杂数据类型,本文以数组,Dictionary,HashTable为例,讲解复杂数据类型的通讯。
(一)数组
1、.NET服务器端程序
假设我们要做段程序,来获取所有的用户姓名。修改上节中的代码,增加一个GetUsers方法。代码如下:
{
string[] array = new string[]{"张三","李四","王五"};
return array;
}
2、Flex客户端程序
在 Design 模式下添加,添加一个 Text文本控件,id为txtUsers,txt属性为空,添加一个 Button控件,id为btnGetArray,Label属性为 GetArray
在 Source 模式下, 修改 mx:RemoteObject 标签,添加
<mx:method name="GetArray" result="RemoteResult(event)" fault="RemoteFault(event)"/>
修改脚本中 RemoteResult 方法,增加一个Case条件,代码如下
{
switch(re.currentTarget.name)
{
case "HelloWord":
var str:String = re.result as String;
this.txtHelloWord.text = str;
break;
case "SayHello":
str = re.result as String;
this.txtSayHello.text = str;
break;
case "GetArray":
for(var i:int=0; i<re.result.length; i++)
{
this.txtUsers.text += re.result[i].toString() + ", ";
}
break;
}
}
运行Flex程序,在浏览器中查看效果 
(二)Dictionary
1、.NET服务器端程序
接下来我们做个Dictionary的例子,假设上例中,我们不光要获取用户姓名,而且我们还要得到他对应的年龄,我们使用Dictionary。修改代码,添加如下方法:
{
Dictionary<String, Int32> age = new Dictionary<string, int>();
age.Add("张三", 23);
age.Add("李四", 24);
age.Add("王五", 22);
return age;
}
2、Flex客户端程序
在 Design 模式下添加,添加六个 Text文本控件,属性分别为 id = txtZhangSan,txt = ""; id = txtLiSi,txt = ""; id = txtWangWu,txt = ""; txt = "张三:"; txt = "李四:"; txt = "王五:"
添加一个 Button控件,id为btnGetDictionary,Label属性为 GetDictionary
在 Source 模式下, 修改 mx:RemoteObject 标签,添加
<mx:method name="GetDictionary" result="RemoteResult(event)" fault="RemoteFault(event)"/>
修改脚本中 RemoteResult 方法,增加一个Case条件,代码如下
{
switch(re.currentTarget.name)
{
case "HelloWord":
var str:String = re.result as String;
this.txtHelloWord.text = str;
break;
case "SayHello":
str = re.result as String;
this.txtSayHello.text = str;
break;
case "GetUsers":
for(var i:int=0; i<re.result.length; i++)
{
this.txtUsers.text += re.result[i].toString() + ", ";
}
break;
case "GetDictionary":
this.txtZhangSan.text = re.result["张三"];
this.txtLiSi.text = re.result["李四"];
this.txtWangWu.text = re.result["王五"];
break;
}
}
运行Flex程序,在浏览器中查看效果 
(三)HashTable
1、.NET服务器端程序
我们将上例中的Dictionary类型改成HashTable类型,代码如下:
{
Hashtable hash = new Hashtable();
hash.Add("张三", 23);
hash.Add("李四", 24);
hash.Add("王五", 22);
return hash;
}
2、Flex客户端程序
借用下上例内容,在 Design 模式下添加一个 Button控件,id为btnGetHashTable,Label属性为 GetHashTable
在 Source 模式下, 修改 mx:RemoteObject 标签,添加
<mx:method name="GetHashTable" result="RemoteResult(event)" fault="RemoteFault(event)"/>
修改脚本中 RemoteResult 方法,只需在上例 case "GetDictionary": 下一行加上 case "GetHashTable": 即可。
在 mx:Button (GetHashTable) 标签中添加属性 click="sampleRemoteObject.GetHashTable()"
运行Flex程序,在浏览器中查看效果
1、.NET端
2、Flex端
本节内容到此结束,其实本节的例子都差不多,相信讲到这里,看过文章的人对Remoting通讯有了大概的了解,只要熟悉AS3语言,以上这些都不困难,关于这个通讯,我计划还有一节,主要讲解自定义实体对象的传送。