2、传递单个实体类,使用WebService中的实体类
这种情况下,可以看作是情况1的特例——只有一个元素的数组。
当然,这种情况下我们可以换一种做法——使用WebService中的实体类。
先看webservice中的代码:
[XmlInclude(typeof(Student))]
[WebMethod]
public string HelloStu(Student stuInfo)
{
return stuInfo.StuName;
}
/**//// <summary>
/// 传递单个实体类,使用WebService中的实体类
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
string str = "";
localhost.Student stuInfo1 = new localhost.Student();//注意,这里调用了webservice中的实体类,而不是Model中的实体类。否则出错。
stuInfo1.StuName = "lxinxuan";
str = ser.HelloStu(stuInfo1);
//传递webservice中的实体类
MessageBox.Show(str);
}
[WebMethod]
public string HelloStusByList(Collection<Student> stuList)//这里参数类型是Collection
{
BLL.Class1 cls = new BLL.Class1();
return cls.GetName(stuList);
}
/**//// <summary>
/// 传递实体类构成的Collection,通过修改Reference.cs的代码,不过每次更新WebService之后都要重新修改,而且必须每个类修改,麻烦
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_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";
Collection<localhost.Student> stuList = new Collection<localhost.Student>();
stuList.Add(stuInfo1);
stuList.Add(stuInfo2);
str = ser.HelloStusByList(stuList);//默认情况下,这里HelloStusByList方法的参数是Student[],通过手动修改为Collection,就可以了
MessageBox.Show(str);
}

