webservice传递的内容必须是可序列化的,不管是参数还是返回值。
所以IList<>不能作为参数和返回值,webservice应该是使用xml来传输数据的 据我猜测
最好是数组 这里事例使用的是实体类的数组作为返回值,使用单个实体类作为参数和返回值更为简单
使用实体类 的方式:
实体类:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace Model
{
[Serializable]//表示该类可序列化
public class Student
{
private string stuName;
public Student()
{ }
public string StuName
{
get { return this.stuName; }
set { this.stuName = value; }
}
}
}
webservice代码:
[XmlInclude(typeof(Student))] //使得调用该服务的人可以使用该代理类
[WebMethod]
public string HelloStus(ArrayList stuList)
{
BLL.Class1 cls = new BLL.Class1();
return cls.GetName(stuList);
}
调用代码:
/// < summary>
/// 必须使用webservice中的实体类,传递实体类集合,作为Object[]传递,WebService中的参数类型是ArrayList,并提供一个将集合转化为Object[]的公共类
/// < /summary>
/// < param name="sender">< /param>
/// < param name="e">< /param>
private void button1_Click(object sender, EventArgs e)
{
string str = "";
localhost.Student stuInfo1 = new localhost.Student();
stuInfo1.StuName = "lxinxuan";
localhost.Student stuInfo2 = new localhost.Student();
stuInfo2.StuName = "www.cnblogs.com/lxinxuan";
IList< localhost.Student> stuList = new List< localhost.Student>();
stuList.Add(stuInfo1);
stuList.Add(stuInfo2);
object[] array = this.ConvertToArray< localhost.Student>(stuList);//这是一个将集合转换为Objec[]的泛型方法
str = ser.HelloStus(array);//传递Object[],返回值是StuName的值
MessageBox.Show(str);
}
//这是一个将集合转换为Objec[]的泛型方法
private object[] ConvertToArray< T>(IList< T> tList)
{
object[] array = new object[tList.Count];
int i = 0;
foreach (T t in tList)
{
array[i] = t;
i++;
}
return array;
}