SilverShaded
Well-known member
- Joined
- Mar 7, 2020
- Messages
- 110
- Programming Experience
- 10+
Hi, i'm want to deserialize an object from a different assembly and came across this example below; however the example refers to " return defaultBinder.BindToType(assemblyName, typeName);" but when i run the code the defaultBinder is null, (new BinaryFormatter().Binder is null)
Is there another way doing this, im aware of typeforwarding but that is not what i want, i want to deserialise an object from a different assembly and where the namespace is different to the original.
Is there another way doing this, im aware of typeforwarding but that is not what i want, i want to deserialise an object from a different assembly and where the namespace is different to the original.
C#:
public class TypeOnlyBinder : SerializationBinder
{
private static SerializationBinder defaultBinder = new BinaryFormatter().Binder;
public override Type BindToType(string assemblyName, string typeName)
{
if (assemblyName.Equals("NA"))
return Type.GetType(typeName);
else
return defaultBinder.BindToType(assemblyName, typeName);
}
public override void BindToName(Type serializedType, out string assemblyName, out string typeName)
{
// but null out the assembly name
assemblyName = "NA";
typeName = serializedType.FullName;
}
private static object locker = new object();
private static TypeOnlyBinder _default = null;
public static TypeOnlyBinder Default
{
get
{
lock (locker)
{
if (_default == null)
_default = new TypeOnlyBinder();
}
return _default;
}
}