【C#】Switch datatype between object and byte[]
2024-09-04 21:21:52
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);
}
最新文章
- POJ1385 计算多边形的重心
- JavaScript之作用域和引用类型
- 浅析C语言指针问题
- Unity3D 学习笔记
- How to install .deb file in Ubuntu
- addEventListener()及attachEvent()区别分析
- OC金额转大写
- Angular 自定义拖拽指令
- layui基础总结
- 【LeetCode】134.Gas Station
- Linux服务器---百科mediawiki
- 12.4 hdfs总结
- FileChannel
- ASIHTTPRequest-断点续传需要原网站支持!
- ConcurrentModificationException 详解
- 2243: [SDOI2011]染色(LCT)
- 管理node.js版本的模块:n
- mysql配置调优-开启慢查询日志-slow_query_log
- [LintCode] 最后一个单词的长度
- 24、Linux 多线程压缩工具pigz 的学习
热门文章
- leetcode第九题--Palindrome Number
- Ubuntu12.04环境搭建遇到的问题和建议(一个)
- Solr 教程
- struts2 &;lt;s: select 标签值
- 【转】浏览器DNS 预取读技术的危害
- Visual Studio 2013 的 Browser Link 功能
- Oracle常用函数:DateDiff() 返回两个日期之间的时间间隔自定义函数
- 体验SubSonic
- linux 启动oracle报cannot restore segment prot after reloc: Permission denied
- PLAN :昔日未来