zoukankan      html  css  js  c++  java
  • C#调用webservice 时如何传递实体对象

    在webservice端公开一个实体类,然后实例化,赋值,然后再给到webservice,可以实现,但是,即使调用端和service端的实体类完全一致,你也要重新实例化service端的,重新赋值,将此传回service端。因为,C#是强类型的语言。

    当然,你也可以通过下面的方法传递实体类,而不用太纠结于赋值了。
    在调用端序列化实体对象(实体对象须是可序列化的类):
     
    public static byte[] SerializeObject(object pObj)
            {
                if (pObj == null)
                    return null;
                System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
                BinaryFormatter formatter = new BinaryFormatter();
                formatter.Serialize(memoryStream, pObj);
                memoryStream.Position = 0;
                byte[] read = new byte[memoryStream.Length];
                memoryStream.Read(read, 0, read.Length);
                memoryStream.Close();
                return read;
            }
     
    在service的调用方法中,接受一个byte[] 参数即可,然后反序列化:
     
    public object DeserializeObject(byte[] pBytes)
            {
                object newOjb = null;
                if (pBytes == null)
                {
                    return newOjb;
                }
                System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(pBytes);
                memoryStream.Position = 0;
                BinaryFormatter formatter = new BinaryFormatter();
                newOjb = formatter.Deserialize(memoryStream);
                memoryStream.Close();
                return newOjb;
            }
     
    然后将object强制转换成相应的实体类型,就可以直接使用此对象了。
    注:在调用端和service端都引用了同一版本的实体程序集.
  • 相关阅读:
    动态规划_leetcode70
    动态规划_leetcode64
    动态规划_leetcode63
    PHP处理base64编码字符串
    PHP解决h5页面跨域
    PHP对象转数组
    jQuery 正则
    mysql重置密码
    yii框架学习(获取插入后的id)
    nginx 之 root和alias
  • 原文地址:https://www.cnblogs.com/shangshen/p/3592218.html
Copyright © 2011-2022 走看看