For one of my object, I need to do some backward/forward compatibility tricks so that everyone is happy while different versions coexist and share the same config file.
The last trick I tried is to have a bridge collection that maps the old format to the new format using an IEnumerable(Of String)
. My problem is that I can't get XmlSerializer
to recognize my custom collection. I looked at the serializer code generating code and everything seems fine. No matter what I do, as long as I use my custom collection ListeCategories
ends up in Unsupported
. If I use a basic List(Of String)
, it works but I miss the logic. What am I missing?
I also tried a non generic custom collection (commented in the second code snippet) with no more success.
Here are the codes for both the main object and the custom collection.
Note that the custom enumerable implements and additional Add
method as required by XmlSerializer
(according to MS Docs).
The XmlSerializer gives special treatment to classes that implement IEnumerable or ICollection. A class that implements IEnumerable must implement a public Add method that takes a single parameter. The Add method's parameter must be of the same type as is returned from the Current property on the value returned from GetEnumerator, or one of that type's bases. A class that implements ICollection (such as CollectionBase) in addition to IEnumerable must have a public Item indexed property (indexer in C#) that takes an integer, and it must have a public Count property of type integer. The parameter to the Add method must be the same type as is returned from the Item property, or one of that type's bases. For classes that implement ICollection, values to be serialized are retrieved from the indexed Item property, not by calling GetEnumerator.
Public Class OptionsSerializable
Public Sub New()
End Sub
<XmlAnyElement>
Public Property Unsupported As List(Of XmlElement)
<System.Xml.Serialization.XmlElement(ElementName:="ListeCategoriesExt")>
Public ReadOnly Property Categories As List(Of CategoryInfo)
Public ReadOnly Property ListeCategories As IEnumerable(Of String) = New EnumerableBridge(Of String, CategoryInfo)(_categories, Function(s) New CategoryInfo(s), Function(c) c.SearchTerm)
Public Property ServeurExchangeUrl As String
Public Property VersionExchange As String
Public Property AdresseBoitePartager As New List(Of String)
Public Property GroupingMasks As New List(Of String)
Public Property TFSLinks As New XmlSerializableDictionary(Of String, String)
Public Property EstAlertageActif As Boolean
Public Property Laps As Integer
Public Property NbErreursMaximum As Integer
Public Property DerniereVerification As Date
Public Property ListeEnvoi As String
Public Property DernierEnvoi As Date
Public Property LogonDernierEnvoi As String
Public Property IntervalEnvoiMinimum As Integer
End Class
'Public Class SpecificCollectionBridge
' Inherits CollectionBridge(Of String, CategoryInfo)
' Public Sub New(refCollection As IEnumerable(Of CategoryInfo))
' MyBase.New(refCollection, Function(s) New CategoryInfo(s), Function(c) c.SearchTerm)
' End Sub
'End Class
<Serializable>
Public Class EnumerableBridge(Of TSource, TTarget)
Implements IEnumerable(Of TSource)
Private _refCollection As IList(Of TTarget)
Private _converterTo As Func(Of TSource, TTarget)
Private _converterFrom As Func(Of TTarget, TSource)
Public Sub New()
End Sub
Public Sub New(refCollection As IList(Of TTarget), converterTo As Func(Of TSource, TTarget), converterFrom As Func(Of TTarget, TSource))
_refCollection = refCollection
_converterTo = converterTo
_converterFrom = converterFrom
End Sub
Public Sub Add(item As TSource) 'Implements ICollection(Of TSource).Add
_refCollection.Add(_converterTo(item))
End Sub
Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
Return GetEnumerator()
End Function
Public Function IEnumerableOfT_GetEnumerator() As IEnumerator(Of TSource) Implements IEnumerable(Of TSource).GetEnumerator
Return _refCollection.Select(_converterFrom).GetEnumerator()
End Function
End Class