System.Runtime.Serialization.Formatters.Binary.BinaryFormatter Class

Serializes and deserializes an object, or an entire graph of connected objects, in binary format.

See Also: BinaryFormatter Members

Syntax

[System.Runtime.InteropServices.ComVisible(true)]
public sealed class BinaryFormatter : System.Runtime.Remoting.Messaging.IRemotingFormatter

Remarks

The System.Runtime.Serialization.Formatters.Soap.SoapFormatter and System.Runtime.Serialization.Formatters.Binary.BinaryFormatter classes implement the System.Runtime.Remoting.Messaging.IRemotingFormatter interface to support remote procedure calls (RPCs), and the System.Runtime.Serialization.IFormatter interface (inherited by the System.Runtime.Remoting.Messaging.IRemotingFormatter) to support serialization of a graph of objects. The System.Runtime.Serialization.Formatters.Soap.SoapFormatter class also supports RPCs with System.Runtime.Serialization.Formatters.ISoapMessage objects, without using the System.Runtime.Remoting.Messaging.IRemotingFormatter functionality.

During RPCs, the System.Runtime.Remoting.Messaging.IRemotingFormatter interface allows the specification of two separate object graphs: the graph of objects to serialize, and an additional graph that contains an array of header objects that convey information about the remote function call (for example, transaction ID or a method signature).

RPCs that use the System.Runtime.Serialization.Formatters.Binary.BinaryFormatter separate into two distinct parts: method calls, which are sent to the server with the remote object that contains the method called, and method responses, which are sent from the server to the client with the status and response information from the called method.

During serialization of a method call the first object of the object graph must support the System.Runtime.Remoting.Messaging.IMethodCallMessage interface. To deserialize a method call, use the BinaryFormatter.Deserialize(System.IO.Stream) method with the System.Runtime.Remoting.Messaging.HeaderHandler parameter. The remoting infrastructure uses the System.Runtime.Remoting.Messaging.HeaderHandler delegate to produce an object that supports the System.Runtime.Serialization.ISerializable interface. When the System.Runtime.Serialization.Formatters.Binary.BinaryFormatter invokes the System.Runtime.Remoting.Messaging.HeaderHandler delegate, it returns the URI of the remote object with the method that is being called. The first object in the graph returned supports the System.Runtime.Remoting.Messaging.IMethodCallMessage interface.

The serialization procedure for a method response is identical to that of a method call, except the first object of the object graph must support the System.Runtime.Remoting.Messaging.IMethodReturnMessage interface. To deserialize a method response, use the BinaryFormatter.DeserializeMethodResponse(System.IO.Stream, System.Runtime.Remoting.Messaging.HeaderHandler, System.Runtime.Remoting.Messaging.IMethodCallMessage) method. To save time, details about the caller object are not sent to the remote object during the method call. These details are instead obtained from the original method call, which is passed to the BinaryFormatter.DeserializeMethodResponse(System.IO.Stream, System.Runtime.Remoting.Messaging.HeaderHandler, System.Runtime.Remoting.Messaging.IMethodCallMessage) method in the System.Runtime.Remoting.Messaging.IMethodCallMessage parameter. The first object in the graph returned by the BinaryFormatter.DeserializeMethodResponse(System.IO.Stream, System.Runtime.Remoting.Messaging.HeaderHandler, System.Runtime.Remoting.Messaging.IMethodCallMessage) method supports the System.Runtime.Remoting.Messaging.IMethodReturnMessage interface.

Unpaired Surrogates

Any unpaired surrogate characters are lost in binary serialization. For example, the following string contains a high surrogate Unicode character (\ud800) in between the two Test words:

Test\ud800Test

Before serialization, the byte array of the string is as follows:

84

T

101

e

115

s

116

t

55296

\ud800

84

T

101

e

115

s

116

t

After deserialization, the high surrogate Unicode character is lost:

84

T

101

e

115

s

116

t

84

T

101

e

115

s

116

t

Requirements

Namespace: System.Runtime.Serialization.Formatters.Binary
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0