Example of a Customer Binary Serializer in .Net
So, I want to implement my own binary se开发者_JAVA技巧rialization. I'm looking for some examples to head me in the right direction.
Also, am I better to make my own serializer class, or just implement ISerializable and use BinarySerializer? If I implement Iserializable, does that get around the assembly loading/version dependency problems with BinarySerializer?
Check out protobuf-net written by Marc Gravell (a Stack Overflow guy)
I would avoid implementing your own, unless you have to of course. The project is open source so you can check it out.
I now use this after getting fed up with BinaryFormatter
Using protobuf is easy and its ultra fast, and it does NOT experience the assembly loading/version dependency problems.
Oh and if you need some performance stats, check this out. Fast!
I asked a similar question about an alternative to BinaryFormatter
Here is an example in vb.net
I converted it from some c# so sorry if it doesn't compile, but will hopefully help you
I posted the c# code also.
If you need to persist the serialized object I recommend using base64 to a type ntext if you're using ms sql.....
My example serializes the whole object this or me in vb.net, however sometimes you may want to have full control or you may run into problems with the security attribute which can cause security issues - if so you can serialize individual fields and remove the serialize attribute. so I included the code to serialize individual fields - they are commented out
There are many ways to serialize objects in .NET, sometimes the attribute Serialize can cause security issues then you have to serialize each field.
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# source
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();
}
}
}
精彩评论