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
BuildingsCollec tion is not supported because it implements IDictionary. Here
is my class:
Namespace BusinessLogicLa yer
<Serializable() > _
Public Class BuildingsCollec tion
Inherits DictionaryBase
Default Property Item(ByVal key As String) As Building
Get
Return Me.Dictionary.I tem(key)
End Get
Set(ByVal Value As Building)
Me.Dictionary.I tem(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.Cont ains(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.Valu es
End Get
End Property
Public Sub Remove(ByVal key As String)
Me.Dictionary.R emove(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 BuildingsCollec tion, since one person could
have more than one building.
Thanks in advance...
- Nick 2 3535
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 NotSupportedExc eption. If there is any
misunderstandin g, 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, XmlSerializatio n 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 SerializableAtt ribute/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 NotSupportedExc eption. If there is any
misunderstandin g, 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, XmlSerializatio n 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 SerializableAtt ribute/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 CustomObject. How would I do this? Thanks in
advance.
Public Class CustomCollection
Inherits Hashtable
' default constructor
|
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 error: The type
BuildingsCollection is not supported because it implements IDictionary. Here
is my class:
Namespace BusinessLogicLayer
<Serializable()> _
Public Class BuildingsCollection
|
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 RecordCriteriaItem. I
want to be able to use a for each loop to loop through the values in the
hash tables (as you can see, each RecordCriteriaItem consists of the related
objects in the three Hashtables). Something like this:
Dim rc as...
|
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 that I need to implement versioning, so I have to implement the
ISerializable interface and provide the GetObjectData function and the
constructor to deserialize the object. When I try to deserialize the member
that is derived from DictionaryBase, I...
|
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 fine. However, when attempting to assing
default values as designtime in the propertygrid, nothing is working on the
class object. I know that I am doing it wrong, any ideas what it is?
Thanks in advance
| |
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 it.
Public Class KeyValue
Dim _keyID As FieldAttributes
Dim _value As FieldAttributes
'public accessors omitted
End Class
Public Class KeyValueDictionary(Of KeyValue) : Inherits DictionaryBase
|
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 nDBKey As Long) As ContentBlock
Get
|
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
'ContentObjects.ContentBlock'."
The error occurs at the "For...Each" line if this method:
|
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: 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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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: 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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
| |
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: 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 we have to send another system
| |