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 2 3522
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."
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." This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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"
|
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...
|
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...
|
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
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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...
| |