Web Service 通过BinaryFormatter序列化和反序列化泛型List
来源:互联网 发布:淘宝店图片轮播制作 编辑:程序博客网 时间:2024/05/15 22:45
1、序列化和反序列化的扩展方法如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Runtime.Serialization.Formatters.Binary;using System.IO;using System.Runtime.Serialization;public static class Extenstions { //序列化 public static byte[] SerializeToByte<T>(this T o) { using (MemoryStream stream = new MemoryStream()) { IFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, o); byte[] buffer = stream.ToArray(); return buffer; } } //反序列化 public static T ByteToDeserialize<T>(this byte[] buffer) { using (MemoryStream stream = new MemoryStream(buffer)) { IFormatter formatter = new BinaryFormatter(); stream.Seek(0, SeekOrigin.Begin); return (T)formatter.Deserialize(stream); } } }2.为要序列化的类加上[Serializable]
[Serializable] public class Tb { public string Id { get; set; } public string Name { get; set; } }3、Web Service 中序列化List
[WebMethod(Description = "")] public byte[] GetTbList() { string sql = "select * from tb"; using (var conn = Database.GetConn()) { List<Tb> list = conn.Query<Tb>(sql).ToList(); byte[] buffer = list.SerializeToByte(); return buffer; } }4、调用Web Service并反序列化为List
private void BindData() { byte[] buffer = TestService.GetTbList(); //TestService为服务实例 List<Tb> list = buffer.ByteToDeserialize<List<Tb>>(); datagridview1.DataSource = list; }
- Web Service 通过BinaryFormatter序列化和反序列化泛型List
- MSDN|BinaryFormatter|序列化和反序列化|c#
- BinaryFormatter 序列化反序列化
- BinaryFormatter 序列化及反序列化
- 二进制BinaryFormatter 泛型 序列化与反序列化 (保存文件到本地和读取)
- C# 二进制BinaryFormatter进行序列化与反序列化
- C# 二进制BinaryFormatter进行序列化与反序列化
- BinaryFormatter序列化与反序列化(C#)
- SoapFormatter 和 BinaryFormatter 序列化对象
- C# ArrayList用BinaryFormatter序列化和反序列化进行文件读写的一个简单例子
- BinaryFormatter序列化
- 序列化之一 BinaryFormatter
- BinaryFormatter.Deserialize 不能反序列化其它Application或者assembly产生的序列化文件
- c#序列化BinaryFormatter和XmlFormatter的区别
- LIST<T>序列化和反序列化
- json序列化和反序列化List
- 使用BinaryFormatter进行序列化
- 通过ObjectOutputStream序列化和使用ObjectInputStream反序列化
- 二维数组中a+i和*(a+i)一样含义解析
- git上传文件
- Web安全测试FAQ
- Uvalive 4865 Data Recovery 最大流
- Linux下安装protobuf并实现简单的客户端服务器端通信
- Web Service 通过BinaryFormatter序列化和反序列化泛型List
- JS 实现点击<a> 标签的时候给其换背景
- hdu 4115 Eliminate the Conflict ( 2-sat )
- CMMI——CM:Configuration Management,配置管理
- SurfaceHolder简述
- java中字符串 MD5密码的加密与验证
- void main(int argc, char *avgv[])的理解
- 我的设计模式学习笔记------>Java设计模式总概况
- 『other』开发者拒绝写技术博客的常见理由