In application called "application1" I serialize this structure into binary:
namespace namespace1
{
[System.Serializeable]
class ClassA
{
List<ClassB> List
}
}
In application called "application2" I have to deserialize this binary but my ClassA and ClassB have to be inside namespace2 instead of namespace1.
So I wrote a serialization binder:
public class TypeNameConverter : SerializationBinder
{
public override Type BindToType(string assemblyName, string typeName)
{
assemblyName = Assembly.GetExecutingAssembly().FullName;
if (typeName.Contains("nemespace1."))
typeName = typeName.Replace("nemespace1.", "nemespace2.");
Type retType = Type.GetType(string.Format("{0}, {1}", typeName, assemblyName));
return retType;
}
}
and I use it like this:
binaryFormatter.Binder = new TypeNameConverter();
(T)binaryFormatter.Deserialize(memoryStream);
And this sucessfully deserializes namespace1.ClassA into namespace2.ClassA, but fails to further deserialize list od nemespace1.ClassB into list of namespace2.ClassB. I guess it fails because list of ClassB is encapsulated into ClassA and BindToType() function does not get called for the "inner elements", it gets called only once for ClassA, then trys to deserialize ClassA and fails to extract list of ClassB with error:
SerializationException: Could not find type
'System.Collections.Generic.List`1[[namespace1.ClassB, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null]]'.
I cannot do any adjustments to application1 (cannot change the way I do serialization) I can only adjust deserialization logic in application2.
I am trying to find the solution for this particular problem, anyone had similar problem or maybe an idea on how to solve this?
Thanks!