CustomSerializationBinder Class

Provides a very simple customizable SerializationBinder that can convert Type to and from string by using assignable delegate properties.

Definition

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

Remarks

When serializing, you can assign the AssemblyNameResolver and TypeNameResolver properties to customize the assembly and type names of a Type to be written into the serialization stream.

When deserializing, you can assign the TypeResolver properties to return a type from an assembly-type name pair.

If the properties above are not assigned or when they return , then the consumer IFormatter instance will use its internal resolve logic.

  Security Note

If TypeResolver is not assigned or can return , then the consumer IFormatter instance may load assemblies during the deserialization. If the deserialization stream is not from a trusted source, then you should never return from the assigned delegate of the TypeResolver property. Instead, throw an exception if a type could not be resolved.

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

Example

C#
// deserializing a renamed type
var formatter = new BinaryFormatter(); // or a BinarySerializationFormatter in non-safe mode
formatter.Binder = new CustomSerializationBinder
{
    TypeResolver = (asmName, typeName) =>
        typeName == "MyNamespace.MyOldClass" ? typeof(MyNewClass) : null
};

return (MyNewClass)formatter.Deserialize(streamContainingOldData);

Constructors

CustomSerializationBinderInitializes a new instance of the CustomSerializationBinder class

Properties

AssemblyNameResolver Gets or sets the custom assembly name resolver logic. It is invoked by the BindToName method. If returns a non- value, then it will be stored as the custom assembly name for the Type specified by the delegate argument.
TypeNameResolver Gets or sets the custom type name resolver logic. It is invoked by the BindToName method. If returns a non- value, then it will be stored as the custom full type name (without the assembly name) for the Type specified by the delegate argument.
TypeResolver Gets or sets the custom Type resolver logic. It is invoked by the BindToType method passing the stored assembly and type names in the delegate arguments, respectively. If returns the formatter will attempt to resolve the names by its default logic.

Methods

BindToName Binds a Type to an assemblyName and typeName. This implementation sets assemblyName by using the AssemblyNameResolver property, and sets typeName by using the TypeNameResolver property.
(Overrides SerializationBinderBindToName(Type, String, String))
BindToType Gets a Type associated by the provided assemblyName and typeName. This implementation uses the TypeResolver property to determine the result Type.
(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