【问题标题】:Example of a Customer Binary Serializer in .Net.Net 中的客户二进制序列化程序示例
【发布时间】:2011-04-14 15:39:58
【问题描述】:

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

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

【问题讨论】:

  • 自定义二进制序列化器的原因是什么?
  • 托马斯+1。如果您需要 XML 看起来非常具体,我可以看到构建自定义 XML 序列化程序,但是对于二进制?您是否正在尝试与一些现有的 API 进行交互,其中将对象表示为二进制数据?在此处需要更多信息
  • 应用程序的一部分作为 AutoCAD 的插件运行。在那种情况下,反序列化总是失败,声称找不到程序集。我知道程序集在那里,因为该程序集的代码必须在它到达这一点之前运行。也有部分好奇心(我喜欢学习新东西)。
  • @drventure,它也是一个性能的东西。我已经有一个自定义的 XML 序列化程序,但是在用户等待它时可能需要 20 秒才能运行。
  • 附带说明,您可以使用 AssemblyLoad 事件修复程序集加载失败。我个人同意@wal,但我有点偏见

标签: .net vb.net binary-serialization


【解决方案1】:

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

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

在厌倦了BinaryFormatter之后,我现在使用它

使用 protobuf 既简单又超快速,而且不会遇到程序集加载/版本依赖问题。

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

我向similar question 询问了BinaryFormatter 的替代方案

【讨论】:

  • 为我节省了一些打字时间 :) 实际上,“v1”代码和“v2”代码在许多方面显示了两种不同的执行方式——重写在内部是相当残酷的。 V1 在 VS2010 中作为“nuget”包提供。
  • 说到 v2...看起来怎么样?
  • 试一试,但您必须标记所有要序列化的字段。它不仅仅是我拥有的大量数据,而且是一个包含许多类的非常深的结构。听起来 v2 可以解决这个问题,但它似乎还没有推出。
  • 这是我的问题的答案,因为这是二进制序列化的一个例子。
【解决方案2】:

这是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();

        }

    }
}

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    • 2014-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多