所以,我想实现自己的二进制序列化。我正在寻找一些例子来引导我朝着正确的方向前进。
另外,我最好制作自己的序列化程序类,还是只实现 ISerializable 并使用 BinarySerializer?如果我实现 Iserializable,是否可以解决 BinarySerializer 的程序集加载/版本依赖问题?
所以,我想实现自己的二进制序列化。我正在寻找一些例子来引导我朝着正确的方向前进。
另外,我最好制作自己的序列化程序类,还是只实现 ISerializable 并使用 BinarySerializer?如果我实现 Iserializable,是否可以解决 BinarySerializer 的程序集加载/版本依赖问题?
查看Marc Gravell(堆栈溢出人员)编写的protobuf-net
我会避免实施你自己的,除非你当然必须这样做。该项目是开源的,因此您可以查看它。
我厌倦了现在使用它BinaryFormatter
使用 protobuf 很简单,而且速度超快,并且不会遇到程序集加载/版本依赖问题。
哦,如果您需要一些性能统计数据,请查看此。快速地!
我问了一个类似的问题,关于替代BinaryFormatter
这是 vb.net 中的一个示例
我从一些 c# 转换它,如果它不能编译,很抱歉,但希望能帮助你
我也发布了c#代码。
如果您需要将序列化对象持久化,如果您使用的是 ms sql,我建议您使用 base64 类型为 ntext .....
我的示例在 vb.net 中序列化了 this 或 me 的整个对象,但是有时您可能想要完全控制,或者您可能会遇到可能导致安全问题的安全属性问题 - 如果是这样,您可以序列化单个字段并删除序列化属性。所以我包含了序列化各个字段的代码 - 它们被注释掉了
在 .NET 中有很多方法可以序列化对象,有时 Serialize 属性会导致安全问题,那么您必须对每个字段进行序列化。
Imports System.Linq
Imports System.Collections.Generic
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO
Namespace ConsoleApp
<Serializable> _
Public Class Example
Public Property S() As String
Get
Return m_S
End Get
Set
m_S = Value
End Set
End Property
Private m_S As String
Public Property I() As Integer
Get
Return m_I
End Get
Set
m_I = Value
End Set
End Property
Private m_I As Integer
Public Property List() As List(Of String)
Get
Return m_List
End Get
Set
m_List = Value
End Set
End Property
Private m_List As List(Of String)
Public Function Pack() As Byte()
Dim bf = New BinaryFormatter()
Using ms = New MemoryStream()
bf.Serialize(ms, Me)
' bf.Serialize(ms, S);
' bf.Serialize(ms, I);
' bf.Serialize(ms, List);
Return ms.ToArray()
End Using
End Function
Public Function UnPack(data As Byte()) As Example
Dim bf = New BinaryFormatter()
Using ms = New MemoryStream(data)
' S = (string) bf.Deserialize(ms);
' I = (int) bf.Deserialize(ms);
' List = (List<string>) bf.Deserialize(ms);
Return DirectCast(bf.Deserialize(ms), Example)
End Using
' return this;
End Function
Public Overrides Function ToString() As String
Return String.Format("[Example: S={0}, I={1}, List={2}]", S, I, [String].Join(",", List.ToArray()))
End Function
End Class
Class MainClass
Public Shared Sub Main(args As String())
Dim o1 = New Example() With { _
Key .S = "James", _
Key .I = 9, _
Key .List = New List(Of String)(New String() {"a", "b"}) _
}
Dim o2 = New Example().UnPack(o1.Pack())
Console.WriteLine(o1.ToString())
Console.WriteLine(o2.ToString())
Console.ReadLine()
End Sub
End Class
End Namespace
C# 源代码
using System;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace ConsoleApp
{
[Serializable()]
public class Example {
public string S {get;set;}
public int I {get;set;}
public List<string> List {get; set;}
public byte[] Pack() {
var bf = new BinaryFormatter();
using (var ms = new MemoryStream()) {
bf.Serialize(ms, this);
// bf.Serialize(ms, S);
// bf.Serialize(ms, I);
// bf.Serialize(ms, List);
return ms.ToArray();
}
}
public Example UnPack(byte[] data) {
var bf = new BinaryFormatter();
using (var ms = new MemoryStream(data)) {
return (Example) bf.Deserialize(ms);
// S = (string) bf.Deserialize(ms);
// I = (int) bf.Deserialize(ms);
// List = (List<string>) bf.Deserialize(ms);
}
// return this;
}
public override string ToString ()
{
return string.Format ("[Example: S={0}, I={1}, List={2}]", S, I, String.Join(",", List.ToArray()));
}
}
class MainClass
{
public static void Main (string[] args)
{
var o1 = new Example() {S = "James", I = 9, List= new List<string>(new string[] {"a", "b"})};
var o2 = new Example().UnPack(o1.Pack());
Console.WriteLine(o1.ToString());
Console.WriteLine(o2.ToString());
Console.ReadLine();
}
}
}