9

After changing the namespace of my class I can no longer deserialize the objects. I've implemented SerializationBinder. Example:

public class TypeNameConverter : SerializationBinder
{
    public override Type BindToType(string assemblyName, string typeName)
    {
        typeName = typeName.Replace("MyOldNamespace", "MyNewNamespace");
        return Type.GetType(string.Format("{0}, {1}", typeName, assemblyName));
    }
}

BinaryFormatter bf = new BinaryFormatter();
bf.Binder = new TypeNameConverter();

The exception I get is:

'System.Runtime.Serialization.TypeLoadExceptionHolder' cannot be converted to type 'MyNewNamespace.MyClass'

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Max Eisenhardt
  • 442
  • 2
  • 9
  • 20
  • Do you need to change the namespace in the assembly too? – d89761 Oct 04 '12 at 23:53
  • not exactly sure what you're talking about. all I did was move my dataBase class out of my project and into it's own project. as far as i know the only thing that changed was the namespace. – Max Eisenhardt Oct 06 '12 at 01:44

1 Answers1

14

you forgot to replace the assembly name:

class TypeNameConverter : SerializationBinder
{
    public override Type BindToType(string assemblyName, string typeName)
    {
        typeName = typeName.Replace("MyOldNamespace", "MyNewNamespace");
        assemblyName = assemblyName.Replace("MyOldNamespace", "MyNewNamespace");
        return Type.GetType(string.Format("{0}, {1}", typeName, assemblyName));
    }
}
Bastianon Massimo
  • 1,710
  • 1
  • 16
  • 23