SerializationBinder
System.Runtime.Serialization Namespace
Summary
When overridden in a derived class, this class controls the binding of serialized objects to their corresponding types during deserialization. This class is abstract.
Remarks
The .NET Framework serialization infrastructure uses the SerializationBinder class to locate and bind types during deserialization. This is particularly useful when types might have moved, been renamed, or are not directly accessible through the standard assembly resolution mechanisms.
You can derive from SerializationBinder and override the BindToType method to provide custom logic for locating and loading types. This is often used in scenarios like:
- Deserializing data from older versions of an assembly where type names or locations have changed.
- Deserializing data across different application domains or assemblies.
- Implementing custom security measures or type validation during deserialization.
To use a custom SerializationBinder, you typically set it on the Formatter instance before calling the deserialization method (e.g., BinaryFormatter.Deserialize).
Inheritance Hierarchy
System.Object
System.Runtime.Serialization.SerializationBinder
Constructors
This class does not have any public constructors.
Methods
BindToType
When overridden in a derived class, controls the binding of serialized objects to their corresponding types during deserialization.
Parameters
| Name | Type | Description |
|---|---|---|
assemblyName |
System.String | The name of the assembly that contains the serialized type. |
typeName |
System.String | The name of the serialized type. |
serializedType |
out System.Type | When this method returns, contains a reference to the type of the serialized object. This parameter is passed uninitialized. |
Return Value
System.Boolean
true if the type was successfully bound; otherwise, false.
Examples
Custom Serialization Binder
The following example demonstrates how to create a custom SerializationBinder to handle type redirection. In this scenario, we're simulating a type that has moved from one assembly to another.
Member Classes
No specific member classes are directly associated with SerializationBinder itself, as it's an abstract base class designed for extension.
See Also
System.Runtime.Serialization Namespace
Provides classes for formatting and serializing objects into a streaming format and deserializing them back into objects.
BinaryFormatter
Serializes and deserializes objects into and from a binary format.
Type.GetType Method
Gets the Type with the specified name, performing a case-sensitive search.