Home > Backend Development > C++ > How to Serialize a Dictionary Member in a Class for Configuration Files?

How to Serialize a Dictionary Member in a Class for Configuration Files?

Mary-Kate Olsen
Release: 2025-01-15 18:27:44
Original
783 people have browsed it

How to Serialize a Dictionary Member in a Class for Configuration Files?

Serialize a class containing dictionary members

When trying to serialize a class that contains dictionary members (such as a configuration file class), you may encounter the following error:

<code>System.NotSupportedException: 无法序列化成员 App.Configfile.mappedDrives</code>
Copy after login

This error means that by default, the Dictionary type cannot be serialized directly. To resolve this issue, consider implementing a custom serialization mechanism.

Custom dictionary serialization

One way is to create a custom class that inherits from Dictionary<TKey, TValue> and implements the IXmlSerializable interface. The following code snippet demonstrates this implementation:

using System;
using System.Collections.Generic;
using System.Xml.Serialization;

[XmlRoot("dictionary")]
public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
{
    public void ReadXml(System.Xml.XmlReader reader)
    {
        XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
        XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));

        bool wasEmpty = reader.IsEmptyElement;
        reader.Read();

        if (wasEmpty)
            return;

        while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
        {
            reader.ReadStartElement("item");

            reader.ReadStartElement("key");
            TKey key = (TKey)keySerializer.Deserialize(reader);
            reader.ReadEndElement();

            reader.ReadStartElement("value");
            TValue value = (TValue)valueSerializer.Deserialize(reader);
            reader.ReadEndElement();

            this.Add(key, value);

            reader.ReadEndElement();
            reader.MoveToContent();
        }
        reader.ReadEndElement();
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
        XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));

        foreach (TKey key in this.Keys)
        {
            writer.WriteStartElement("item");

            writer.WriteStartElement("key");
            keySerializer.Serialize(writer, key);
            writer.WriteEndElement();

            writer.WriteStartElement("value");
            TValue value = this[key];
            valueSerializer.Serialize(writer, value);
            writer.WriteEndElement();

            writer.WriteEndElement();
        }
    }
}
Copy after login

Custom dictionaries can participate in XML serialization by implementing IXmlSerializable.

Usage

Once a custom dictionary is defined, it can be used to store the required key-value pairs in the profile class:

public class ConfigFile
{
    ...
    public SerializableDictionary<string, string> mappedDrives = new SerializableDictionary<string, string>();
    ...
}
Copy after login

With this approach, configuration file classes can be serialized and deserialized without encountering serialization errors.

The above is the detailed content of How to Serialize a Dictionary Member in a Class for Configuration Files?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template