当前位置: 首页 > 新闻动态 > 软件编程

c#泛型序列化对象为字节数组的示例

作者:用户投稿 浏览: 发布日期:2026-01-16
[导读]:这篇文章主要介绍了c#泛型序列化对象为字节数组的示例,需要的朋友可以参考下

序列化对象为字节数组
复制代码 代码如下:
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
        protected byte[] Serialize<T>(T t)
        {
            MemoryStream mStream = new MemoryStream();
            BinaryFormatter bFormatter = new BinaryFormatter();
            bFormatter.Serialize(mStream, t);
            return mStream.GetBuffer();
        }

反序列化字节数组为对象
复制代码 代码如下:
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
        protected T Deserialize<T>(byte[] b)
        {
            BinaryFormatter bFormatter = new BinaryFormatter();
            return (T)bFormatter.Deserialize(new MemoryStream(b));
        }

免责声明:转载请注明出处:http://m.lexweb.cn/news/308337.html

扫一扫高效沟通

多一份参考总有益处

免费领取网站策划SEO优化策划方案

请填写下方表单,我们会尽快与您联系
感谢您的咨询,我们会尽快给您回复!