.Net中的客户二进制序列化程序示例

时间:2011-04-14 15:39:59

标签: .net vb.net binary-serialization

所以,我想实现自己的二进制序列化。我正在寻找一些例子让我朝着正确的方向前进。

另外,我最好制作自己的序列化程序类,还是只实现ISerializable并使用BinarySerializer?如果我实现了Iserializable,是否可以解决BinarySerializer的程序集加载/版本依赖性问题?

2 个答案:

答案 0 :(得分:2)

查看由Marc Gravell撰写的protobuf-net(Stack Overflow家伙)

我会避免实施你自己的,除非你必须这样做。该项目是开源的,因此您可以查看它。

我现在在厌倦了BinaryFormatter

后使用它

使用protobuf非常简单且速度极快,并且不会遇到装配加载/版本依赖性问题。

哦,如果你需要一些性能统计数据,请检查this。快!

我向similar question询问了BinaryFormatter

的替代方法

答案 1 :(得分:0)

以下是vb.net中的一个示例

我将它从某些c#转换为对不起,如果它不能编译,但希望能帮到你

我也发布了c#代码。

如果你需要保留序列化对象,我推荐使用base64到类型ntext,如果你使用ms sql .....

我的示例在vb.net中序列化了这个或我的整个对象,但有时你可能想要完全控制,或者你可能遇到安全属性的问题,这可能导致安全问题 - 如果是这样你可以序列化单个字段和删除序列化属性。 所以我包含了序列化各个字段的代码 - 它们被注释掉了

有许多方法可以在.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#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();

        }

    }
}