不同Assembly中使用 BinaryFormatter 序列化的问题


Unity游戏项目.在PVE验证时.将验证信息序列化.发送到服务器端.在服务器端反序列化.并进行验证.为了共享结构.使用同一份代码.在服务器端使用了中间语言.直接调用C#代码.


序列化过程为方便使用了 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/



  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值