.NET API Reference

Microsoft Documentation

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:

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.

public abstract bool BindToType(string assemblyName, string typeName, out Type serializedType);
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.

using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; // Assume this is the original type definition public class OldNamespace.MyClass { public int Value { get; set; } } // Assume this is the new type definition after a refactoring public class NewNamespace.MyClass { public int Value { get; set; } } public class MyCustomBinder : SerializationBinder { public override bool BindToType(string assemblyName, string typeName, out Type serializedType) { serializedType = null; if (typeName.StartsWith("OldNamespace.MyClass")) { // Redirect to the new type in NewNamespace string newTypeName = typeName.Replace("OldNamespace", "NewNamespace"); // Attempt to bind using standard assembly resolution for the new type // You might need to explicitly load assemblies here if they are not in the probing path serializedType = Type.GetType($"{newTypeName}, YourNewAssemblyName"); // Replace YourNewAssemblyName if (serializedType != null) { Console.WriteLine($"Successfully bound '{typeName}' to '{serializedType.FullName}'"); return true; } } // Fallback to default binding if not handled return false; } } public class Program { public static void Main(string[] args) { // --- Serialization --- var formatter = new BinaryFormatter(); // Ensure to set the binder during serialization if you want to record it for later binding // In this example, we're focusing on deserialization binding. // Simulate serialization of an object of the old type // This part would typically happen in an older version of your application. // For demonstration, let's just imagine it's serialized. // --- Deserialization with Custom Binder --- // Imagine 'serializedStream' contains the binary data of an OldNamespace.MyClass object. // In a real scenario, you'd read this from a file or network stream. // Mocking a serialized stream for demonstration purposes // In a real app, this would be a stream from a file or network. // This requires knowing the actual assembly name where OldNamespace.MyClass was serialized. // For simplicity, we'll assume it's serialized as is and our binder will find the new one. // This is a placeholder. Actual serialization and deserialization would be more complex. Console.WriteLine("Simulating deserialization with a custom binder..."); // Example of how you would use the binder: // formatter.Binder = new MyCustomBinder(); // object obj = formatter.Deserialize(serializedStream); // Console.WriteLine($"Deserialized object type: {obj.GetType().FullName}"); Console.WriteLine("\nCustom binder example setup complete. Actual deserialization requires serialized data."); } }

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.