This sample shows how to turn object to byte[], as well as turn byte[] to object.

So,I can turn any types of object into byte[],which can be saved and transported properly.

Attention!Attention!Attention!(Important things should be repeated three times:))

1.Don't forget to using <System.IO; System.Runtime.Serialization.Formatters.Binary; System.Runtime.Serialization;> at the beginning of your program.

2.When you need to transport an class/struct that defined by yourself, you'd better put the definition into a DLL file and using it in your program.

3.Also,put a [Serializable] before the definition(if it is defined on your own).

 public static byte[] Object2Bytes(object obj)
{
IFormatter fmt = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
fmt.Serialize(ms,obj);
return ms.GetBuffer();
} public static object Bytes2Object(byte[] bt)
{
IFormatter fmt = new BinaryFormatter();
MemoryStream ms = new MemoryStream(bt);
return (object)fmt.Deserialize(ms);
}

最新文章

  1. POJ1385 计算多边形的重心
  2. JavaScript之作用域和引用类型
  3. 浅析C语言指针问题
  4. Unity3D 学习笔记
  5. How to install .deb file in Ubuntu
  6. addEventListener()及attachEvent()区别分析
  7. OC金额转大写
  8. Angular 自定义拖拽指令
  9. layui基础总结
  10. 【LeetCode】134.Gas Station
  11. Linux服务器---百科mediawiki
  12. 12.4 hdfs总结
  13. FileChannel
  14. ASIHTTPRequest-断点续传需要原网站支持!
  15. ConcurrentModificationException 详解
  16. 2243: [SDOI2011]染色(LCT)
  17. 管理node.js版本的模块:n
  18. mysql配置调优-开启慢查询日志-slow_query_log
  19. [LintCode] 最后一个单词的长度
  20. 24、Linux 多线程压缩工具pigz 的学习

热门文章

  1. leetcode第九题--Palindrome Number
  2. Ubuntu12.04环境搭建遇到的问题和建议(一个)
  3. Solr 教程
  4. struts2 &amp;lt;s: select 标签值
  5. 【转】浏览器DNS 预取读技术的危害
  6. Visual Studio 2013 的 Browser Link 功能
  7. Oracle常用函数:DateDiff() 返回两个日期之间的时间间隔自定义函数
  8. 体验SubSonic
  9. linux 启动oracle报cannot restore segment prot after reloc: Permission denied
  10. PLAN :昔日未来