c#泛型序列化对象为字节数组的示例
程序员文章站
2023-12-18 21:03: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));
}
推荐阅读