Class Serializer
The main entry point into the serialization subsystem. Provides methods to serialize, deserialize and clone objects.
Inheritance
Namespace: Microsoft.Psi
Assembly: Microsoft.Psi.dll
Syntax
public static class Serializer : object
Remarks
For efficiency reasons the Serializer doesn't serialize any type information, and as a result it requires the user to identify the type being deserialized. In the case of polymorphic fields, the serializer collects type information in the Schema instance passed in to Serialize. The caller needs to provide this type info back when calling Deserialize.
Methods
View SourceClear<T>(ref T, SerializationContext)
Clears the instance in preparation for caching it for future reuse as a cloning or deserialization target. The method is expected to call Serializer.Clear on all reference-type fields. This method is for custom serializers.
Declaration
public static void Clear<T>(ref T target, SerializationContext context)
Parameters
Type | Name | Description |
---|---|---|
T | target | The instance to clear. |
Serialization |
context | A context object containing accumulated type mappings and object references. |
Type Parameters
Name | Description |
---|---|
T | The type of object to clear. |
Clone<T>(T, ref T, SerializationContext)
Makes a deep clone of the given object graph into the target object graph, avoiding any allocations, provided that the target object graph has the same shape. Call this override from within custom serializers.
Declaration
public static void Clone<T>(T instance, ref T target, SerializationContext context)
Parameters
Type | Name | Description |
---|---|---|
T | instance | The instance to clone. |
T | target | An existing instance to clone into. |
Serialization |
context | An optional serialization context. |
Type Parameters
Name | Description |
---|---|
T | The type of object to clone. |
DeepClone<T>(T)
Creates a deep clone of the given object. Except for the case of simple value types, this method allocates a new object tree to clone into. This can become a performance bottleneck when the clone operation needs to be executed many times. In these cases, the other Clone overrides which avoid allocations perform significantly better.
Declaration
public static T DeepClone<T>(this T instance)
Parameters
Type | Name | Description |
---|---|---|
T | instance | The instance to clone. |
Returns
Type | Description |
---|---|
T | The deep-clone. |
Type Parameters
Name | Description |
---|---|
T | The type of object to clone. |
DeepClone<T>(T, ref T)
Makes a deep clone of the given object graph into the target object graph, avoiding any allocations (provided that the target object tree has the same shape).
Declaration
public static void DeepClone<T>(this T instance, ref T target)
Parameters
Type | Name | Description |
---|---|---|
T | instance | The instance to clone. |
T | target | An existing instance to clone into. |
Type Parameters
Name | Description |
---|---|
T | The type of object to clone. |
DeepClone<T>(T, IRecyclingPool<T>)
Creates a deep clone of the given object. The method will clone into an unused target instance obtained from the specified recycler. The caller should return the clone to the recycler when done.
Declaration
public static T DeepClone<T>(this T instance, IRecyclingPool<T> recycler)
Parameters
Type | Name | Description |
---|---|---|
T | instance | The instance to clone. |
IRecycling |
recycler | An object recycling cache. |
Returns
Type | Description |
---|---|
T | The deep-clone. |
Type Parameters
Name | Description |
---|---|
T | The type of object to clone. |
Deserialize<T>(BufferReader, ref T, SerializationContext)
Deserializes the given instance from the specified stream. Call this override from within custom serializers.
Declaration
public static void Deserialize<T>(BufferReader reader, ref T target, SerializationContext context)
Parameters
Type | Name | Description |
---|---|---|
Buffer |
reader | The stream reader to deserialize from. |
T | target | An optional existing instance to clone into. |
Serialization |
context | A context object containing accumulated type and object references. |
Type Parameters
Name | Description |
---|---|
T | The type of object to deserialize. |
IsImmutableType<T>()
Returns true if the type is immutable (it is a primitive type or all its fields are read-only immutable types).
Declaration
public static bool IsImmutableType<T>()
Returns
Type | Description |
---|---|
System. |
True if the type is immutable. |
Type Parameters
Name | Description |
---|---|
T | The type to analyze. |
Serialize<T>(BufferWriter, T, SerializationContext)
Serializes the given instance to the specified stream. Call this override from within custom serializers.
Declaration
public static void Serialize<T>(BufferWriter writer, T instance, SerializationContext context)
Parameters
Type | Name | Description |
---|---|---|
Buffer |
writer | The stream writer to serialize to. |
T | instance | The instance to serialize. |
Serialization |
context | A context object containing accumulated type and object references. |
Type Parameters
Name | Description |
---|---|
T | The type of object to serialize. |