反序列化无法找到程序集
提示找不到程序集.
原因是序列化时把序列化类的命名空间等信息保存了,但应用程序和类库的命名空间可能是
不一样的,所以提示找不到程序集.
在反序列化使用的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);
}
}