System.Runtime.Serialization.IFormatter Interface

Assembly: Mscorlib.dll
Namespace: System.Runtime.Serialization
Summary
Provides functionality for formatting serialized objects.
C# Syntax:
public interface IFormatter
Remarks
This interface must be implemented by any class identified as a formatter in the System.Runtime.Serialization architecture.

Objects controlling their own serialization can do so by implementing the ISerializable interface. In order for an object to be serialized, you must mark that object as being serializable. You can do this by applying the serializable attribute to a class. If any object in the graph is not serializable, serialization will fail.



Notes to implementors: All formatters must implement this interface. Use IFormatter.Serialize to serialize an object or graph of objects. Use IFormatter.Deserialize to deserialize a stream and create a clone of the original object or graph of objects.
See also:
System.Runtime.Serialization Namespace See also:
MSDN: serialization | Formatter | ISerializable

System.Runtime.Serialization.IFormatter Member List:

Public Properties
Binder Read-write

Gets or sets the SerializationBinder that performs type lookups during deserialization.
Context Read-write

Gets or sets the StreamingContext used for serialization and deserialization.
SurrogateSelector Read-write

Gets or sets the SurrogateSelector used by the current formatter.
Public Methods
Deserialize Deserializes the data on the provided stream and reconstitutes the graph of objects.
Serialize Serializes an object, or graph of objects with the given root to the provided stream.

System.Runtime.Serialization.IFormatter Member Details

Property: Binder (read-write)
Summary
Gets or sets the SerializationBinder that performs type lookups during deserialization.
C# Syntax:
SerializationBinder Binder {get; set;}
Remarks
When trying to resolve a type from information encoded on the stream, the formatter calls the SerializationBinder.BindToType method on the SerializationBinder. This method resolves these parameters to a Type object. The binder can find a Type at deserialization time that is in a different assembly than it was at serialization time.

Setting this property has no effect during serialization.

Return to top


Property: Context (read-write)
Summary
Gets or sets the StreamingContext used for serialization and deserialization.
C# Syntax:
StreamingContext Context {get; set;}
Remarks
The value of this property is passed to any object implementing ISerializable or ISerializationSurrogate. The StreamingContext indicates the destination (during serialization) or the source (during deserialization) of the data. An object implementing ISerializable can alter the data that it transmits depending on value of the IFormatter.Context.

Return to top


Property: SurrogateSelector (read-write)
Summary
Gets or sets the SurrogateSelector used by the current formatter.
C# Syntax:
ISurrogateSelector SurrogateSelector {get; set;}
Remarks
An ISerializationSurrogate allows the user to specify an object best suited to handle the serialization of a particular object or class of objects. Think of it as an implementation of ISerializable but provided by a different object.

Return to top


Method: Deserialize(
   Stream serializationStream
)
Summary
Deserializes the data on the provided stream and reconstitutes the graph of objects.
C# Syntax:
object Deserialize(
   Stream serializationStream
);
Parameters:

serializationStream

The stream containing the data to deserialize.

Return Value:
The top object of the deserialized graph.
Remarks
The IFormatter.Deserialize method reads graph information from the stream and reconstructs a clone of the original graph. The topology of the graph is preserved.

The deserialization process allocates an empty object of the appropriate type and repopulates its fields from the data transmitted in the serializationStream stream. It is important to note that no constructor is ever called on the object during deserialization.

See also:
MSDN: serialization

Return to top


Method: Serialize(
   Stream serializationStream,
   object graph
)
Summary
Serializes an object, or graph of objects with the given root to the provided stream.
C# Syntax:
void Serialize(
   Stream serializationStream,
   object graph
);
Parameters:

serializationStream

The stream where the formatter puts the serialized data. This stream can reference a variety of backing stores (such as files, network, memory, and so on).

graph

The object, or root of the object graph, to serialize. All child objects of this root object are automatically serialized.

Remarks
The IFormatter.Serialize method automatically serializes the provided objects, and all objects connected to it, to the provided stream.

By default, the serialization process records an object's state by gathering the values of all its fields (public and private). These fields are saved to the stream along with information about the object such as the assembly-qualified name for its type.

See also:
MSDN: serialization

Return to top


Top of page

Copyright (c) 2002 Microsoft Corporation. All rights reserved.