472,778 Members | 2,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,778 software developers and data experts.

Serialization of an object containing LinkedList

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?

Jan 4 '06 #1
2 9004
Justin,

You shouldn't have to do any of that. Unless you have some other reason
for implementing custom serialization, the LinkedList<int> should serialize
just fine with using the Serializable attribute and nothing else.

I ran a test here serializing a class which contained a LinkedList and
it worked just fine.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Justin Crites" <jc*****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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?

Jan 4 '06 #2
What about serialization of a LinkedList of List<T> where T is either a
primitive type or a class? In both cases, I get the following exception when
creating the XmlSerializer() instance:

There was an error reflecting type 'HoldsLinkedList'. --->
System.InvalidOperationException: You must implement a default accessor on
System.Collections.Generic.LinkedList`1[[System.Collections.Generic.List`1[[System.Int32,
mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089]] because it inherits from
ICollection.

where

public class HoldsLinkedList
{
private LinkedList<List<int>> list;

public LinkedList<List<int>> List
{
get { return list; }
set { list = value; }
}
}

"Nicholas Paldino [.NET/C# MVP]" wrote:
Justin,

You shouldn't have to do any of that. Unless you have some other reason
for implementing custom serialization, the LinkedList<int> should serialize
just fine with using the Serializable attribute and nothing else.

I ran a test here serializing a class which contained a LinkedList and
it worked just fine.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Justin Crites" <jc*****@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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?


Jan 20 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bjorn | last post by:
I know you should be careful before blaming internal frameworks, but this definitely does look like an internal bug. When serializing a ChildClass that inherits a BaseClass containing a struct...
2
by: divya_rathore_ | last post by:
Dear All, Assuming that I have an object of a templated class of Linked List: LinkedList<int> IntList which adds entries using a member function like this: void AppendEntry(T& entry) and...
2
by: Ray Mitchell | last post by:
Hello, I am currently doing default serialization of various objects and I am aware that the simplest way to do serialization between assemblies is to put the objects to be serialized in a...
3
by: Cederstrom | last post by:
Hello group! :) I want to create a config/status object, that will contain a Hashtable. This object I would like to save through Serialization, but im having some trouble. My hashtable contain...
0
by: Abhishek Srivastava | last post by:
Hello All, I am writing a XML WebService. I have two very simpilar methods exposed as webService. GetPersonList and GetModuleList. The GetPersonList method returns an ArrayList containing...
4
by: Aaron Clamage | last post by:
Hi, Could someone please confirm the following? I think I have found a subtle .NET serialization bug. It occurs when object has a list of items containing another object of the same type and...
3
by: GreyAlien007 | last post by:
I extended the class TreeNode to add some properties of my liking. Anyway, no problems there, I can add my derived TreeNode into TreeNodeCollections and use the properties etc. However, when I...
5
by: Spam Catcher | last post by:
Hi All, I have a question concerning serialization in .NET. If I serialize an object to a database and then I change the object in code (i.e. add a property, rename a property, delete a...
1
by: kikisan | last post by:
I am developing a windows service which utilizes the following classes: interface IPersistable; abstract class PersistableObject : IPersistable;
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.