不同Assembly中使用 BinaryFormatter 序列化的问题
2015-11-10 17:22
447 查看
Unity游戏项目.在PVE验证时.将验证信息序列化.发送到服务器端.在服务器端反序列化.并进行验证.为了共享结构.使用同一份代码.在服务器端使用了中间语言.直接调用C#代码.
序列化过程为方便使用了 BinaryFormatter
反序列化时遇到问题.在反序列化结构时抛出异常
An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll
google下来的理解为 BinaryFormatter 存储类型信息时存储了当前集合的类型信息.这样导致序列化出来的信息只能在同一个程序集内使用.
好在提供了可以重写的SerializationBinder
使用时指定
然而在序列化容器时会遇到
unable to load type system.collections.generic.list`1 的异常.
没能找到解决方案.看来需要自己序列化容器.
算了.还是换个序列化方式吧..
参考
https://www.daniweb.com/programming/software-development/threads/339638/deserializing-in-a-different-assembly http://stackoverflow.com/questions/14990980/system-runtime-serialization-serializationexception-unable-to-find-assembly-mya http://spazzarama.com/2009/06/25/binary-deserialize-unable-to-find-assembly/
序列化过程为方便使用了 BinaryFormatter
反序列化时遇到问题.在反序列化结构时抛出异常
An unhandled exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll
google下来的理解为 BinaryFormatter 存储类型信息时存储了当前集合的类型信息.这样导致序列化出来的信息只能在同一个程序集内使用.
好在提供了可以重写的SerializationBinder
<span style="font-size:18px;"> class BindChanger : System.Runtime.Serialization.SerializationBinder { public override Type BindToType(string assemblyName, string typeName) { // Define the new type to bind to Type typeToDeserialize = null; // Get the assembly names string currentAssembly = Assembly.GetExecutingAssembly().FullName; // Create the new type and return it typeToDeserialize = Type.GetType(string.Format("{0}, {1}", typeName, currentAssembly)); return typeToDeserialize; } }</span>
使用时指定
<span style="font-size:18px;"> BinaryFormatter bin = new BinaryFormatter(); bin.Binder = new BindChanger();</span>
然而在序列化容器时会遇到
unable to load type system.collections.generic.list`1 的异常.
没能找到解决方案.看来需要自己序列化容器.
算了.还是换个序列化方式吧..
参考
https://www.daniweb.com/programming/software-development/threads/339638/deserializing-in-a-different-assembly http://stackoverflow.com/questions/14990980/system-runtime-serialization-serializationexception-unable-to-find-assembly-mya http://spazzarama.com/2009/06/25/binary-deserialize-unable-to-find-assembly/
相关文章推荐
- C#自定义序列化ISerializable的实现方法
- c#数据的序列化和反序列化(推荐版)
- c#序列化详解示例
- c#对象反序列化与对象序列化示例详解
- .net实现序列化与反序列化实例解析
- C#实现复杂XML的序列化与反序列化
- C#中Serializable序列化实例详解
- C#实现的json序列化和反序列化代码实例
- C#序列化与反序列化(Serialize,Deserialize)实例详解
- C#二进制序列化实例分析
- asp.net 序列化and反序列化演示
- Json序列化和反序列化方法解析
- 深入理解Java对象的序列化与反序列化的应用
- 使用 ServiceStack.Text 序列化 json的实现代码
- php 中序列化和json使用介绍
- Android中的序列化浅析
- Android xml文件的序列化实现代码
- unity实现多点触控代码
- 详解PHP序列化反序列化的方法
- jquery序列化form表单使用ajax提交后处理返回的json数据