KGy SOFT

CustomSerializationBinder Class

KGy SOFT Core Libraries Help
Provides a very simple customizable SerializationBinder that can convert Type to and from string by using assignable delegate properties.
See the Remarks section for details.
Inheritance Hierarchy

SystemObject
  System.Runtime.SerializationSerializationBinder
    KGySoft.Serialization.BinaryCustomSerializationBinder

Namespace:  KGySoft.Serialization.Binary
Assembly:  KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 6.0.0-rc.1
Syntax

public sealed class CustomSerializationBinder : SerializationBinder, 
	ISerializationBinder

The CustomSerializationBinder type exposes the following members.

Constructors

  NameDescription
Public methodCustomSerializationBinder
Initializes a new instance of the CustomSerializationBinder class
Top
Properties

  NameDescription
Public propertyAssemblyNameResolver
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.
Public propertyTypeNameResolver
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.
Public propertyTypeResolver
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.
Top
Methods

  NameDescription
Public methodBindToName
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).)
Public methodBindToType
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).)
Top
Extension Methods

  NameDescription
Public Extension MethodConvert(Type, CultureInfo)Overloaded.
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.)
Public Extension MethodCode exampleConvertTTarget(CultureInfo)Overloaded.
Converts an Object specified in the obj parameter to the desired TTarget.
(Defined by ObjectExtensions.)
Public Extension MethodIn (Defined by ObjectExtensions.)
Public Extension MethodTryConvert(Type, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvert(Type, CultureInfo, Object)Overloaded.
Tries to convert an Object specified in the obj parameter to the desired targetType.
(Defined by ObjectExtensions.)
Public Extension MethodTryConvertTTarget(TTarget)Overloaded.
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.)
Public Extension MethodTryConvertTTarget(CultureInfo, TTarget)Overloaded.
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.)
Top
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 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.

Examples

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

return (MyNewClass)formatter.Deserialize(streamContainingOldData);
Tip Tip
If the inner structure of the type has also been changed, then you can use the CustomSerializerSurrogateSelector class.
See Also

Reference