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; }
}
}
}
[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;
}
[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);
}
[WebMethod]
public string HelloStusByCollection(string sxml)
{
BLL.Class1 cls = new BLL.Class1();
Collection<Student> stuList = cls.DeSerializerCollection<Student>(sxml, typeof(Collection<Student>));//先反序列化为Collection
return cls.GetName(stuList);
}
/**//// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="sxml"></param>
/// <param name="type"></param>
/// <returns></returns>
public Collection<T> DeSerializerCollection<T>(string sxml, Type type)
{
xmlReader reader = xmlReader.Create(new StringReader(sxml));
System.xml.Serialization.xmlSerializer serializer = new System.xml.Serialization.xmlSerializer(type);
object obj = serializer.Deserialize(reader);
return (Collection<T>)obj;
}
/**//// <summary>
/// 先将实体类集合序列化为string,然后在WebService中反序列化成Collection<>,然后再传递给业务层对象
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
string str = "";
Student stuInfo1 = new Student();
stuInfo1.StuName = "lxinxuan";
Student stuInfo2 = new Student();
stuInfo2.StuName = "www.cnblogs.com/lxinxuan";
Collection<Student> stuList = new Collection<Student>();
stuList.Add(stuInfo1);
stuList.Add(stuInfo2);
string stuString = this.Serializer<Collection<Student>>(stuList);//先序列化为xml文件格式的string
str = ser.HelloStusByCollection(stuString);
MessageBox.Show(str);
}
/**//// <summary>
/// 实体类集合序列化为字符串
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="objToxml"></param>
/// <returns></returns>
public string Serializer<T>(T objToxml)
{
System.IO.StringWriter writer = new System.IO.StringWriter();
System.xml.Serialization.xmlSerializer serializer = new System.xml.Serialization.xmlSerializer(objToxml.GetType());
serializer.Serialize(writer, objToxml);
return writer.GetStringBuilder().ToString();
}