使用 binaryformatter 反序列化
Deserialization with binaryformatter
我有一个序列化对象并通过网络发送它的程序:
TcpClient client = new TcpClient();
client.ReceiveTimeout = 10000;
client.SendTimeout = 10000;
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);
client.Connect(serverEndPoint);
BinaryFormatter binaryformatter = new BinaryFormatter();
NetworkStream networkStream = client.GetStream();
if (networkStream.CanWrite)
{
binaryformatter.Serialize(networkStream, kort);
}
另一方面,我接收并反序列化代码:
TcpClient tcpClient = (TcpClient)client;
tcpClient.SendTimeout = 10000;
tcpClient.ReceiveTimeout = 10000;
NetworkStream clientStream = tcpClient.GetStream();
try
{
if (clientStream.CanRead)
{
BinaryFormatter binaryformatter = new BinaryFormatter();
binaryformatter.Binder = new AllowAllAssemblyVersionsDeserializationBinder();
Kort tempkort = (Kort)binaryformatter.Deserialize(clientStream);
SetImage(tempkort);
}
}
catch (SerializationException e)
{
MessageBox.Show("Failed to deserialize. Reason: " + e.Message);
throw;
}
finally
{
clientStream.Close();
tcpClient.Close();
}
但是当我反序列化时,我得到了关于程序集丢失的错误:
"An unhandled exception of type System.Runtime.Serialization.SerializationException
occurred in Server.exe Additional information: Unable to find assembly 'Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
我用这个解决了:
sealed class AllowAllAssemblyVersionsDeserializationBinder : SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
String currentAssembly = Assembly.GetExecutingAssembly().FullName;
// In this case we are always using the current assembly
typeName = "Server.Kort";
assemblyName = currentAssembly;
// Get the type using the typeName and assemblyName
Type typeToDeserialize = Type.GetType(String.Format("{0}, {1}",
typeName, assemblyName));
return typeToDeserialize;
}
}
但现在我尝试这样做时,我不断收到一条错误消息:
"Object of type Server.Kort
cannot be converted to type Server.Kort+kortvalör
."
而且我不知道如何修复它。
发送端的 class Kort
必须包含一个名为 kortvalör
的嵌套类型实例(可能是 enum
?)。而且,由于 BinaryFormatter
序列化 public & private fields 而不是属性,嵌套类型可能对外界完全不可见,但仍会被序列化。
例如,我能够通过以下 class:
使用您的活页夹重现您的异常 "Object of type Server.Kort cannot be converted to type Server.Kort+kortvalör"
[Serializable]
public class Kort
{
// Private enum that is invisible to the outside world.
enum kortvalör
{
Zero,
One,
Two,
Three
}
kortvalör valör = kortvalör.Three;
public int Value
{
get
{
return (int)valör;
}
set
{
// Check to make sure the incoming value is in a valid range.
var newvalör = (kortvalör)value;
if (Enum.IsDefined(typeof(kortvalör), newvalör))
valör = newvalör;
else
valör = default(kortvalör);
}
}
}
当反序列化上面的 class 时,您的绑定器将被调用两次,一次使用 typeName
for Kort
-- 然后一次使用类型名称 "MyClientNamespace.Kort+kortvalör"
。由于您的活页夹忽略传入的 typeName
和 returns typeof(Kort)
,因此失败。
你有几个选项可以解决这个问题:
将您的 class Kort
提取到共享 DLL 中,并 link 将其与发送和接收应用程序一起使用。然后问题就解决了。
在发送和接收应用程序中创建 Kort
引用的所有类型的副本——包括私有嵌套类型——并在更智能的 SerializationBinder
。文章 Advanced Binary Serialization: Deserializing an Object Into a Different Type Than the One It was Serialized Into 有一个如何做的例子。
考虑使用不同的序列化格式来序列化 properties 而不是私有字段。 BSON is one option. Protobuf-net 是另一个。
我有一个序列化对象并通过网络发送它的程序:
TcpClient client = new TcpClient();
client.ReceiveTimeout = 10000;
client.SendTimeout = 10000;
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888);
client.Connect(serverEndPoint);
BinaryFormatter binaryformatter = new BinaryFormatter();
NetworkStream networkStream = client.GetStream();
if (networkStream.CanWrite)
{
binaryformatter.Serialize(networkStream, kort);
}
另一方面,我接收并反序列化代码:
TcpClient tcpClient = (TcpClient)client;
tcpClient.SendTimeout = 10000;
tcpClient.ReceiveTimeout = 10000;
NetworkStream clientStream = tcpClient.GetStream();
try
{
if (clientStream.CanRead)
{
BinaryFormatter binaryformatter = new BinaryFormatter();
binaryformatter.Binder = new AllowAllAssemblyVersionsDeserializationBinder();
Kort tempkort = (Kort)binaryformatter.Deserialize(clientStream);
SetImage(tempkort);
}
}
catch (SerializationException e)
{
MessageBox.Show("Failed to deserialize. Reason: " + e.Message);
throw;
}
finally
{
clientStream.Close();
tcpClient.Close();
}
但是当我反序列化时,我得到了关于程序集丢失的错误:
"An unhandled exception of type
System.Runtime.Serialization.SerializationException
occurred in Server.exe Additional information: Unable to find assembly 'Client, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
我用这个解决了:
sealed class AllowAllAssemblyVersionsDeserializationBinder : SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
String currentAssembly = Assembly.GetExecutingAssembly().FullName;
// In this case we are always using the current assembly
typeName = "Server.Kort";
assemblyName = currentAssembly;
// Get the type using the typeName and assemblyName
Type typeToDeserialize = Type.GetType(String.Format("{0}, {1}",
typeName, assemblyName));
return typeToDeserialize;
}
}
但现在我尝试这样做时,我不断收到一条错误消息:
"Object of type
Server.Kort
cannot be converted to typeServer.Kort+kortvalör
."
而且我不知道如何修复它。
发送端的 class Kort
必须包含一个名为 kortvalör
的嵌套类型实例(可能是 enum
?)。而且,由于 BinaryFormatter
序列化 public & private fields 而不是属性,嵌套类型可能对外界完全不可见,但仍会被序列化。
例如,我能够通过以下 class:
使用您的活页夹重现您的异常"Object of type Server.Kort cannot be converted to type Server.Kort+kortvalör"
[Serializable]
public class Kort
{
// Private enum that is invisible to the outside world.
enum kortvalör
{
Zero,
One,
Two,
Three
}
kortvalör valör = kortvalör.Three;
public int Value
{
get
{
return (int)valör;
}
set
{
// Check to make sure the incoming value is in a valid range.
var newvalör = (kortvalör)value;
if (Enum.IsDefined(typeof(kortvalör), newvalör))
valör = newvalör;
else
valör = default(kortvalör);
}
}
}
当反序列化上面的 class 时,您的绑定器将被调用两次,一次使用 typeName
for Kort
-- 然后一次使用类型名称 "MyClientNamespace.Kort+kortvalör"
。由于您的活页夹忽略传入的 typeName
和 returns typeof(Kort)
,因此失败。
你有几个选项可以解决这个问题:
将您的 class
Kort
提取到共享 DLL 中,并 link 将其与发送和接收应用程序一起使用。然后问题就解决了。在发送和接收应用程序中创建
Kort
引用的所有类型的副本——包括私有嵌套类型——并在更智能的SerializationBinder
。文章 Advanced Binary Serialization: Deserializing an Object Into a Different Type Than the One It was Serialized Into 有一个如何做的例子。考虑使用不同的序列化格式来序列化 properties 而不是私有字段。 BSON is one option. Protobuf-net 是另一个。