Reputation: 7941
I need to deserialize a binary file created by an other assembly. Type names are the same so on BindToType I alter the full name of the class and return a found type. It works ok, but there are some classes I don't support and I need to ignore them. If I return null, an exception is thrown as the class is not found.
The intermediate error was "SerializationException: The ObjectManager found an invalid number of fixups. This usually indicates a problem in the Formatter.".
How can I ignore unknown types and get the object deserialized in the same moment?
sealed class MyAsemblyBinder : System.Runtime.Serialization.SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
string myAsm = System.Reflection.Assembly.GetExecutingAssembly().FullName;
Type foundType = Type.GetType(String.Format("{0}, {1}", typeName, myAsm));
return foundType;
}
}
Upvotes: 0
Views: 1021
Reputation: 7941
Ignoring types during the serialization is possible by forcing a cast to a generic object during the deserialization. To do that the binder must return type of object.
Full code would look like this:
sealed class MyAsemblyBinder : System.Runtime.Serialization.SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
string myAsm = System.Reflection.Assembly.GetExecutingAssembly().FullName;
Type foundType = Type.GetType(String.Format("{0}, {1}", typeName, myAsm));
if (foundType == null)
foundType = typeof(Object);
return foundType;
}
}
Upvotes: 1