Custom class inherits DictionaryBase

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nick

    #1

    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
    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


  • Kevin Yu [MSFT]

    #2
    RE: Custom class inherits DictionaryBase

    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."

    Comment

    Working...