D
David Sworder
Hi,
I've created a UserControl-derived class called MyUserControl that is
able to persist and subsequently reload its state. It exposes two methods as
follows:
public void Serialize(Stream s);
public void Deserialize(Stream s);
Within the MyUserControl class, there is a field of type MyInnerClass
which is defined as follows:
private MyInnerClass MyInner=null;
...where MyInnerClass is defined as:
[Serializable()]
private class MyInnerClass{
public int Field;
public String Str;
}
When MyUserControl.Serialize() is called, 'MyInner' might be null or it
might not be. When it's not null, serialization takes place properly. When
it *is* null, serialization fails with an exception because
BinaryFormatter.Serialize() expects a non-null parameter.
What is the appropriate design pattern to use when serializing fields
which might be null? I'd like to avoid having to serialize an extra 'bool'
flag that indicates whether the field is null or not. Any ideas?
Thanks in advance,
David
I've created a UserControl-derived class called MyUserControl that is
able to persist and subsequently reload its state. It exposes two methods as
follows:
public void Serialize(Stream s);
public void Deserialize(Stream s);
Within the MyUserControl class, there is a field of type MyInnerClass
which is defined as follows:
private MyInnerClass MyInner=null;
...where MyInnerClass is defined as:
[Serializable()]
private class MyInnerClass{
public int Field;
public String Str;
}
When MyUserControl.Serialize() is called, 'MyInner' might be null or it
might not be. When it's not null, serialization takes place properly. When
it *is* null, serialization fails with an exception because
BinaryFormatter.Serialize() expects a non-null parameter.
What is the appropriate design pattern to use when serializing fields
which might be null? I'd like to avoid having to serialize an extra 'bool'
flag that indicates whether the field is null or not. Any ideas?
Thanks in advance,
David