/// <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;
}