I am implementing a hierarchy of classes that use .NET's built in serialization. Due to the nature of most of these classes and the way .NET handles deserialization with a graph deserializer, I need to store temporary data somewhere and wait for it to be fully deserialized.
My classes all currently include something similar to the following:
class Example : ISerializable
{
private readonly dynamic _serialData = new ExpandoObject();
public ulong Id { get; private set; }
public string[] Strings { get; private set; }
protected Example( SerializationInfo info, StreamingContext context )
{
_serialData.Id = info.GetValue( nameof( Id ), typeof( ulong ) );
_serialData.Strings = info.GetValue( nameof( Strings ), typeof( string[] ) );
}
public void GetObjectData( SerializationInfo info, StreamingContext context )
{
info.AddValue( nameof( Id ), Id );
info.AddValue( nameof( Strings ), Strings );
}
[OnDeserialized]
private void OnDeserialized( StreamingContext context )
{
Id = _serialData.Id;
Strings = _serialData.Strings;
}
}
I realize that the OnDeserialized()
method can sometimes be redundant for primitives and other non-enumerable types, but I am trying to keep everything uniform with the more complex classes that have references to child classes and so on.
With that, here's my question:
I currently store the temporary data in the _serialData
field. Once it is deserialized, there is no need for this field anymore. I could easily just clear the field once deserialization is complete, but I would much rather have it stored somewhere as a scoped variable that is garbage collected when it is no longer needed.
I see that the StreamingContext
is passed along with the serialization constructor and methods, but I don't really understand what it is used for, and I don't see any way to store temporary data in it.
Is there a way I can avoid having _serialData
as a class field entirely?