徐胜利

导航

webservice里面传递自定义类的方法

第一步:首先在你自定义类上加上[Serializable],让你的类支持序列化;

              例如自定义类为student

             [Serializable]

             public class Student

             {

                   public string Name{get;set;}

                   public string Age{get;set;}

             }

第二步:在WebService.cs里面的方法上加入[XmlInclude(typeof(Student))]

            XmlInclude属性需引用命名空间:System.Xml.Serialization;

            例如:

           [XmlInclude(typeof(Student))]

           [WebMethod]

           public  Student [] GetStudent(System.Collections.ObjectModel.Collection<Student> list)

           {

                    Student [] sp = new Student[list.Count];
                    for (int i = 0; i < sp.Length;i++)
                    {
                         sp[0] = (Student)(list[i]);
                    }

                    return Student;          

           }

第三步:在客户端调用的时候应该这样声明

               异步调用需配置:需选择集合类型为:System.Collections.ObjectModel.Collection

              xx.Student stu1 = new xx.Student();

              stu1.Name = "123";

              stu1.Age  = "21";

              xx.Student stu2 = new xx.Student();

              stu2.Name = "456";

              stu2.Age = "21";

              IList<xx.Student> list = new List<xx.Student>();

              list.Add(stu1);

              list.Add(stu2);

              System.Collections.ObjectModel.Collection<xx.Student> listArray = new  System.Collections.ObjectModel.Collection<xx.Student>(list);

             client.GetStudentAsync(listArray);

posted on 2011-11-17 13:45  xuvictory  阅读(626)  评论(0)    收藏  举报