欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-22 14:42:34
序列化对象为字节数组复制代码 代码如下:using system.io;using system.runtime.serialization.formatters.bina...

序列化对象为字节数组

复制代码 代码如下:

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));
        }