SerializationOnGetSerializationState Class

Namespace

System.Runtime.Serialization

Assembly

mscorlib.dll

Syntax

public sealed class SerializationOnGetSerializationState : ISerializable, IObjectReference

Inheritance

Summary

The SerializationOnGetSerializationState class provides a mechanism for objects to supply custom state during deserialization. It is primarily used by advanced serialization scenarios such as object reference tracking and custom object creation.

Members

NameTypeDescription
GetObjectDatavoidImplements ISerializable.GetObjectData to populate a SerializationInfo with data needed for deserialization.
GetRealObjectobjectImplements IObjectReference.GetRealObject to return the actual object that should be deserialized.
SerializationStateobjectGets or sets the custom state object used during serialization.

Constructor

public SerializationOnGetSerializationState(object state)

Initializes a new instance of the SerializationOnGetSerializationState class with the specified state object.

Remarks

This class is intended for developers who need fine‑grained control over the serialization process. It can be used to preserve additional information that is not captured by default serialization behavior.

Example

using System;
using System.Runtime.Serialization;

[Serializable]
public class MyData : ISerializable
{
    public int Id { get; set; }
    public string Name { get; set; }

    protected MyData(SerializationInfo info, StreamingContext context)
    {
        Id = info.GetInt32(nameof(Id));
        Name = info.GetString(nameof(Name));
        var customState = (SerializationOnGetSerializationState)info.GetValue("CustomState", typeof(SerializationOnGetSerializationState));
        // Use customState.SerializationState as needed
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue(nameof(Id), Id);
        info.AddValue(nameof(Name), Name);
        var state = new SerializationOnGetSerializationState(new { Timestamp = DateTime.UtcNow });
        info.AddValue("CustomState", state);
    }
}

See Also