如何解决.Net中的客户二进制序列化程序示例
| 因此,我想实现自己的二进制序列化。我正在寻找一些例子,以使我朝正确的方向前进。 另外,我更好地制作自己的序列化器类,还是仅实现ISerializable并使用BinarySerializer?如果我实现Iserializable,那么BinarySerializer可以解决程序集加载/版本依赖问题吗?解决方法
查看由Marc Gravell(堆栈溢出专家)编写的protobuf-net
我会避免自己实施,除非您必须这样做。该项目是开源的,因此您可以签出。
我现在厌倦了
BinaryFormatter
之后就使用它
使用protobuf非常简单,而且速度超快,并且不会遇到程序集加载/版本依赖问题。
哦,如果您需要一些性能统计信息,请检查一下。快速!
我问了一个类似的问题,关于ѭ0的替代方案
,这是vb.net中的示例
我是从某些C#转换过来的,如果无法编译,抱歉,希望对您有所帮助
我也张贴了C#代码。
如果您需要持久化序列化的对象,如果您使用的是ms sql,建议将base64的类型设置为ntext。
我的示例在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#源
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},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();
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。