J
Justin Crites
I have an object which I want to be serializable. I have marked with
with [SerializableAttribute]. The object only has a single data
member, which is a LinkedList<int>. This linked list is a private
member and cannot be exposed publically without violating encapsulation
of the class.
If I make the class derive from ISerializable and
IDeserializationCallback, I can attempt to "forward" these methods onto
the linked list. For example (assuming _list is our LinkedList<int>):
void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext context) {
_list.GetObjectData(info, context);
}
void IDeserializationCallback.OnDeserialization(object sender)
{
_list.OnDeserialization(sender);
}
This works fine. However, there's a problem: LinkedList does not
publically expose the necessary constructor. ISerializable requires
that the type have implemented a special constructor for
deserialization. This constructor takes the same arguments as
GetObjectData(), and I can implement that in my type, but what do I do?
So that's the question: how can I implement a serializable type that
contains a LinkedList?
with [SerializableAttribute]. The object only has a single data
member, which is a LinkedList<int>. This linked list is a private
member and cannot be exposed publically without violating encapsulation
of the class.
If I make the class derive from ISerializable and
IDeserializationCallback, I can attempt to "forward" these methods onto
the linked list. For example (assuming _list is our LinkedList<int>):
void ISerializable.GetObjectData(SerializationInfo info,
StreamingContext context) {
_list.GetObjectData(info, context);
}
void IDeserializationCallback.OnDeserialization(object sender)
{
_list.OnDeserialization(sender);
}
This works fine. However, there's a problem: LinkedList does not
publically expose the necessary constructor. ISerializable requires
that the type have implemented a special constructor for
deserialization. This constructor takes the same arguments as
GetObjectData(), and I can implement that in my type, but what do I do?
So that's the question: how can I implement a serializable type that
contains a LinkedList?