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 Examples section for an example.
Inheritance Hierarchy

SystemObject
  System.Runtime.SerializationSerializationBinder
    KGySoft.Serialization.BinaryCustomSerializationBinder

Namespace:  KGySoft.Serialization.Binary
Assembly:  KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 5.0.0
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).)
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
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
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