I have an object which I want to be serializable. I have marked with
with [SerializableAtt ribute]. 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
IDeserializatio nCallback, I can attempt to "forward" these methods onto
the linked list. For example (assuming _list is our LinkedList<int> ):
void ISerializable.G etObjectData(Se rializationInfo info,
StreamingContex t context) {
_list.GetObject Data(info, context);
}
void IDeserializatio nCallback.OnDes erialization(ob ject sender)
{
_list.OnDeseria lization(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? 2 9109
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.co m
"Justin Crites" <jc*****@gmail. com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. . I have an object which I want to be serializable. I have marked with with [SerializableAtt ribute]. 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 IDeserializatio nCallback, I can attempt to "forward" these methods onto the linked list. For example (assuming _list is our LinkedList<int> ):
void ISerializable.G etObjectData(Se rializationInfo info, StreamingContex t context) { _list.GetObject Data(info, context); }
void IDeserializatio nCallback.OnDes erialization(ob ject sender) { _list.OnDeseria lization(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?
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 'HoldsLinkedLis t'. --->
System.InvalidO perationExcepti on: You must implement a default accessor on
System.Collecti ons.Generic.Lin kedList`1[[System.Collecti ons.Generic.Lis t`1[[System.Int32,
mscorlib, Version=2.0.0.0 , Culture=neutral ,
PublicKeyToken= b77a5c561934e08 9]], mscorlib, Version=2.0.0.0 ,
Culture=neutral , PublicKeyToken= b77a5c561934e08 9]] 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.co m
"Justin Crites" <jc*****@gmail. com> wrote in message news:11******** **************@ g49g2000cwa.goo glegroups.com.. .I have an object which I want to be serializable. I have marked with with [SerializableAtt ribute]. 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 IDeserializatio nCallback, I can attempt to "forward" these methods onto the linked list. For example (assuming _list is our LinkedList<int> ):
void ISerializable.G etObjectData(Se rializationInfo info, StreamingContex t context) { _list.GetObject Data(info, context); }
void IDeserializatio nCallback.OnDes erialization(ob ject sender) { _list.OnDeseria lization(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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 that contains an object (phew..) the serialization fails.
Error message: "FieldInfo does not match the target Type"
I can serialize the struct, serialize the baseclass (containing the struct),
serialize the childclass when its empty, but _not_...
|
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 retrieves any entry using:
T* FindEntry(int pos);
|
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 common assembly. However, I would like to be
able to put these objects in separate assemblies, but beyond that, continue
doing default serialization. My rough understanding of what I must do is to
implement the ISerializable interface for such...
|
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 some objects, from a class I created myself.
I read that I need to make my own "add" method, which I hopefully did
correct. I hope someone can spot my wrongdoings, and point me in the right
direction. The code is in the bottom of this post:
|
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 PersonVO(id,
name, fullname) object. And GetModuleList returns and ArrayList
containing the ModuleVO (id, module name) object.
The GetPersonList works absolutely fine. But the GetModule List throws
| |
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 both
objects have a non-static member reference to some other static object. In
this case, I get an InvalidArgument exception when I try to access the
non-static member of the child class. I've included a code sample to
clarify. This "bug" is...
|
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 serialize a class containing my new
TreeNode, it doesn't seem to save all the properties back up the inheritance
chain. The class that is getting serialized has one member to be serialized,
a Dictionary<string, List<IOTreeNode>> where IOTreeNode is...
|
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 property) will my object
deserialize correctly?
Will it throw an exception - or will it deserialize whatever it can?
|
by: kikisan |
last post by:
I am developing a windows service which utilizes the following classes:
interface IPersistable;
abstract class PersistableObject : IPersistable;
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |