ForwardedTypesSerializationBinder Class

Provides a SerializationBinder that makes possible to serialize and deserialize types with custom assembly identity.

Definition

Namespace: KGySoft.Serialization.Binary
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 8.1.0
C#
public sealed class ForwardedTypesSerializationBinder : SerializationBinder, 
	ISerializationBinder
Inheritance
Object    SerializationBinder    ForwardedTypesSerializationBinder
Implements
ISerializationBinder

Remarks

  Security Note

If a deserialization stream may come from an untrusted source, then make sure to set the SafeMode property to to prevent loading assemblies and resolving any types but the explicitly specified ones.

See the security notes at the Remarks section of the BinarySerializationFormatter class for more details.

Without specifying any expected types the ForwardedTypesSerializationBinder does nothing special. Resolving types from legacy assemblies works automatically if at least a chunk version of the assembly exists on the current platform containing nothing but a bunch of TypeForwardedToAttribute attributes (this is the case for the original .NET Framework assemblies on .NET Core and .NET Standard).

To resolve types that are not forwarded by the TypeForwardedToAttribute from an existing assembly with the given identity you can use this binder for deserialization. Add the types to be handled by the AddType or AddTypes methods.

If WriteLegacyIdentity is set to , then mapping works also on serialization. For types without an explicitly set mapping the value of the TypeForwardedFromAttribute attribute will be written if it is defined.

To serialize types with arbitrary assembly identity, use this binder both for serialization and deserialization. Add the type to be handled by the AddType method and specify at least one AssemblyName for each added Type.

Example

The following example demonstrates the usage of the ForwardedTypesSerializationBinder when types have to be deserialized from a stream, which have been originally serialized by another version of the assembly.

C#
// If SafeMode is true you must declare every type name that can occur in the serialization stream;
// otherwise, just the ones with changed identity. 
var binder = new ForwardedTypesSerializationBinder { SafeMode = true };

// MyType will be able to be deserialized if the assembly name in the
// serialization stream matches any of the enlisted ones.
binder.AddType(typeof(MyType),
    new AssemblyName("MyOldAssembly, Version=1.0.0.0"),
    new AssemblyName("MyOldAssembly, Version=1.2.0.0"),
    new AssemblyName("MyNewAssembly, Version=1.5.0.0"),
    new AssemblyName("MyNewAssembly, Version=2.0.0.0"));

// MyOtherType will be able to be deserialized if it was serialized by any versions of MyAssembly.
binder.AddType(typeof(MyOtherType), new AssemblyName("MyAssembly"));

// Any type of any assembly will be mapped to SomeOtherType if their full names match.
binder.AddType(typeof(SomeOtherType));

// Multiple types can be enlisted without assembly identity
binder.AddTypes(typeof(MyType), typeof(MyOtherType), typeof(SomeOtherType));

IFormatter formatter = new BinarySerializationFormatter { Binder = binder }; // or BinaryFormatter
object result = formatter.Deserialize(serializationStream);

The following example demonstrates how to control the serialized assembly name.

C#
// Setting the WriteLegacyIdentity allows to use arbitrary custom indenity on serialization.
var binder = new ForwardedTypesSerializationBinder { WriteLegacyIdentity = true };

// When serializing a MyType instance, it will be saved with the firstly specified identity
binder.AddType(typeof(MyType),
    new AssemblyName("MyOldAssembly, Version=1.0.0.0"),
    new AssemblyName("MyOldAssembly, Version=1.2.0.0"),
    new AssemblyName("MyNewAssembly, Version=1.5.0.0"),
    new AssemblyName("MyNewAssembly, Version=2.0.0.0"));

// If WriteLegacyIdentity is true, types with TypeForwardedFromAttribute will be automatically written
// with their old identity stored in the TypeForwardedFromAttribute.AssemblyFullName property.
// List<T> has a TypeForwardedFromAttribute in .NET Core and Standard so it will be serialized with
// mscorlib assembly identity in every platform:
var obj = new List<MyType> { new MyType() };

IFormatter formatter = new BinaryFormatter { Binder = binder }; // or BinarySerializationFormatter
formatter.Serialize(serializationStream, obj);

Constructors

ForwardedTypesSerializationBinderInitializes a new instance of the ForwardedTypesSerializationBinder class

Properties

SafeMode Gets or sets whether all of the type names that occur in the serialization stream must have a defined mapping to a type.
Default value: .
WriteLegacyIdentity Gets or sets whether a legacy assembly identity is tried to be written on serializing.
Default value: .

Methods

AddType Adds the type to this binder. If no assemblyIdentities are defined, then any Type with the same full name will be resolved to type; otherwise, only the ones, whose identities match.
AddTypes Adds the types to this binder without any specific assembly identity. Each Type that have the same full name as one of the given types, will be able to be resolved from any assembly.
BindToName When WriteLegacyIdentity is , then tries to return a legacy assembly identity for the specified serializedType.
(Overrides SerializationBinderBindToName(Type, String, String))
BindToType Retrieves a type by its assemblyName and typeName. If SafeMode is , then an explicit mapping must exist that can be added by the AddType or AddTypes methods.
(Overrides SerializationBinderBindToType(String, String))

Extension Methods

Convert Converts an Object specified in the obj parameter to the desired targetType.
See the Examples section of the generic ConvertTTarget(Object, CultureInfo) overload for an example.
(Defined by ObjectExtensions)
ConvertTTarget Converts an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions)
In Gets whether item is among the elements of set.
See the Examples section of the generic InT(T, T) overload for an example.
(Defined by ObjectExtensions)
TryConvert Tries to convert an Object specified in the obj parameter to the desired targetType.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)
TryConvert Tries to convert an Object specified in the obj parameter to the desired targetType.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)
TryConvertTTarget Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)
TryConvertTTarget Tries to convert an Object specified in the obj parameter to the desired TTarget.
See the Examples section of the ConvertTTarget(Object, CultureInfo) method for a related example.
(Defined by ObjectExtensions)

See Also