本文转自:
http://blog.csdn.net/scgyyu/archive/2009/06/01/4232371.aspx
提示找不到程序集,原因是序列化时把序列化类的命名空间等信息保存了,但应用程序和类库的命名空间可能是
不一样的,所以提示找不到程序集。解决方法如下:
1.将dll加入强名称,注册到全局程序集缓存中
2.在反序列化使用的IFormatter 对象加入Binder 属性,使其获取要反序列化的对象所在的程序集,示例如下:
public void DeSerialize( byte [] data, int offset)
{
IFormatter formatter = new BinaryFormatter();
formatter.Binder = new UBinder();
MemoryStream stream = new MemoryStream(data, offset, stringlength);
this .m_bodyobject = ( object )formatter.Deserialize(stream);
}
public class UBinder:SerializationBinder
{
public override Type BindToType( string assemblyName, string typeName)
{
Assembly ass = Assembly.GetExecutingAssembly();
return ass.GetType(typeName);
}
}
{
IFormatter formatter = new BinaryFormatter();
formatter.Binder = new UBinder();
MemoryStream stream = new MemoryStream(data, offset, stringlength);
this .m_bodyobject = ( object )formatter.Deserialize(stream);
}
public class UBinder:SerializationBinder
{
public override Type BindToType( string assemblyName, string typeName)
{
Assembly ass = Assembly.GetExecutingAssembly();
return ass.GetType(typeName);
}
}