473,503 Members | 2,167 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Custom class inherits DictionaryBase

I am writing an application that uses a custom class which inherits
DictionaryBase so that I can add items with an associated key. When I try to
serialize the object though, I get the following error: The type
BuildingsCollection is not supported because it implements IDictionary. Here
is my class:

Namespace BusinessLogicLayer

<Serializable()> _
Public Class BuildingsCollection
Inherits DictionaryBase

Default Property Item(ByVal key As String) As Building
Get
Return Me.Dictionary.Item(key)
End Get
Set(ByVal Value As Building)
Me.Dictionary.Item(key) = Value
End Set
End Property

Public Sub Add(ByVal item As Building, ByVal key As String)
Dictionary.Add(key, item)
End Sub

Public Function Contains(ByVal key As String) As Boolean
Return Dictionary.Contains(key)
End Function

Public ReadOnly Property Keys() As ICollection
Get
Return Dictionary.Keys
End Get
End Property

Public ReadOnly Property Values() As ICollection
Get
Return Dictionary.Values
End Get
End Property

Public Sub Remove(ByVal key As String)
Me.Dictionary.Remove(key)
End Sub

End Class

End Namespace

Do I have to do anything special to it to allow me to serialize to XML? The
Building object seen in the class is just what I want my collection to be of
so other objects will not work. The building class will work with
serialization. In my main app I have a "Person" class defined with the
following: Private pBuildings as BuildingsCollection, since one person could
have more than one building.

Thanks in advance...

- Nick
Nov 22 '05 #1
2 3527
Hi,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you're trying to serialize a Collection
object into Xml and get a NotSupportedException. If there is any
misunderstanding, please feel free to let me know.

One of the problems will be that the serialization/deserialization does not
preserve identity, so the keys of the deserialized IDictionary will not
match the original. This is because they will be constructed anew.

Also, XmlSerialization is schema driven, and XmlSerializer will attempt to
infer schema from the given type, however there is no obvious way to infer
schema from IDictionary: it is not strongly typed. A strongly typed
collection that is explicitly stated the type of object it collects.

Moreover, XmlSerializer serializes by reflecting public properties. It
doesn't use the SerializableAttribute/ISerializable framework. IDictionary
doesn't expose each element as a public property, so it can't get at the
items that are contained inside. Also, indexed properties can not be stored
either, because they require the serializer to know all of the keys, which
it can not.

A workaround would be wrap your IDictionary in a class that
serializes/deserializes it into a name/value pair array or a jagged object
array with 2 dimensions or some other convenient structure. In the client,
you can deserialize the object back into something that implements
IDictionary. Your wrapper class only needs to wrap IDictionary, for
serialization.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 22 '05 #2
Hi,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you're trying to serialize a Collection
object into Xml and get a NotSupportedException. If there is any
misunderstanding, please feel free to let me know.

One of the problems will be that the serialization/deserialization does not
preserve identity, so the keys of the deserialized IDictionary will not
match the original. This is because they will be constructed anew.

Also, XmlSerialization is schema driven, and XmlSerializer will attempt to
infer schema from the given type, however there is no obvious way to infer
schema from IDictionary: it is not strongly typed. A strongly typed
collection that is explicitly stated the type of object it collects.

Moreover, XmlSerializer serializes by reflecting public properties. It
doesn't use the SerializableAttribute/ISerializable framework. IDictionary
doesn't expose each element as a public property, so it can't get at the
items that are contained inside. Also, indexed properties can not be stored
either, because they require the serializer to know all of the keys, which
it can not.

A workaround would be wrap your IDictionary in a class that
serializes/deserializes it into a name/value pair array or a jagged object
array with 2 dimensions or some other convenient structure. In the client,
you can deserialize the object back into something that implements
IDictionary. Your wrapper class only needs to wrap IDictionary, for
serialization.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 22 '05 #3

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

Similar topics

5
2169
by: rufus | last post by:
Hi, I have a custom collection class that inherits from HashTable. When I add a new CustomObject to my CustomCollection I would like to ensure that it will only accept objects of type...
1
623
by: Nick | last post by:
I am writing an application that uses a custom class which inherits DictionaryBase so that I can add items with an associated key. When I try to serialize the object though, I get the following...
2
1334
by: Rick Palmer | last post by:
The code below is a class I am creating in order to help facilitate a generic "data layer" assembly. Anyway, as you see the RecordCriteria class contains three hashtables, and a structure called...
0
1135
by: a | last post by:
Hi, I am using a SoapFormatter to serialize a class which has a member that is derived from DictionaryBase. Everything works fine... I can serialize and deserialize. The trouble I have is...
2
2418
by: AMDRIT | last post by:
Hello everyone, I have created a custom component and one of its properties is a class object with it's own properties. During runtime, I can assign values to the class object properties just...
1
1016
by: Dan Holmes | last post by:
Given the Class named KeyValue is the declaration for the Dictionary version correct? I can't get the IDE to like it. This is VS2005 beta 2. I think i am doing something wrong but i don't see...
6
2686
by: Mike | last post by:
Hello guys, how are you? I implemented a class called MyDictionary that inherits from DictionaryBase. The thing is that my class, cannot handle a duplicate value in the "keys"
4
6150
by: Martin Widmer | last post by:
Hi folks. I am using this collection class: Public Class ContentBlocksCollection Inherits DictionaryBase 'Object variables for attributes 'Attributes Default Public Property Item(ByVal...
1
4492
by: Martin Widmer | last post by:
Hi Folks. When I iterate through my custom designed collection, I always get the error: "Unable to cast object of type 'System.Collections.DictionaryEntry' to type...
1
1540
by: steven scaife | last post by:
Hi I have created a class that inherits from dictionarybase code below. Public Class FrmLabels Inherits Label Private m_Lbl As Label Public Property lbl() As Label Get Return m_Lbl End Get
0
7353
jinu1996
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...
1
7011
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...
0
7468
tracyyun
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...
0
5596
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5023
isladogs
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...
0
4689
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...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
747
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.