1

I have a custom Dictionary that implements IXmlSerializable and i'm trying to create a custom attribute that can be applied to properties like XmlElementAttribute can :

    <XmlElement>
    Property Cake As Bread

A new XmlDictionaryAttribute, like this :

    <XmlDictionary(Root:="IDs", Item:="ID", Key:="Key", Value:="Value", KeyIsAttribute:=True, ValueIsAttribute:=True)>
    Property IDs As New Dictionary(Of String, String)

To give me alot more flexibility in how i want them Serialized/Deserialized

The Problem is that i don't know how to get the IDs property Dictionary during Serialization without specifying the parent class directly

IXmlSerializable's ReadXml looks like this :

    Public Sub ReadXml(Reader As System.Xml.XmlReader) Implements IXmlSerializable.ReadXml

Which doesn't seem to give any context to the parent class owning the Dictionary property.

How would i get the Attribute values during serialization class-independent like XmlElementAttribute already does it?

VampireMonkey
  • 177
  • 2
  • 11
  • You at least know how to get attributes from reflection (which would potentially help with serialization, but not necessarily deserialization), right? – Craig Oct 15 '20 at 13:22
  • 1
    @Craig Yes but not without specifying the parent class – VampireMonkey Oct 15 '20 at 15:19
  • I don't believe that information is passed in. Since you're subclassing `Dictionary` anyway, you could subclass it further and add the necessary metadata as class attributes, rather than property attributes. – dbc Oct 15 '20 at 16:05
  • In a serialization context, you don't need to know the class, you can just ask the type system for info on the object attributes (though I think you do need to have the classes for the attributes you're looking for up-front). Again, though, I'm not sure how (or if) it would work in a deserialization context. It might be instructive to look that the reference source for the built-in deserialization machinery, if it's available. – Craig Oct 15 '20 at 17:34
  • There seems to be some reflection going on with XmlReflectionImporter in the XmlSerializer at initiation, tho i still need to get that during the ReadXml call https://github.com/microsoft/referencesource/blob/3b1eaf5203992df69de44c783a3eda37d3d4cd10/System.Xml/System/Xml/Serialization/XmlSerializer.cs – VampireMonkey Oct 15 '20 at 22:24

0 Answers0