webservice c#初涉

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;

  }

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值