Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Create one of your own :-), the readonly feature is bonus but if you need a key other than a string then the class needs some modifications... </p> <pre><code>namespace MyNameSpace { [XmlRoot("SerializableDictionary")] public class SerializableDictionary : Dictionary&lt;String, Object&gt;, IXmlSerializable { internal Boolean _ReadOnly = false; public Boolean ReadOnly { get { return this._ReadOnly; } set { this.CheckReadOnly(); this._ReadOnly = value; } } public new Object this[String key] { get { Object value; return this.TryGetValue(key, out value) ? value : null; } set { this.CheckReadOnly(); if(value != null) { base[key] = value; } else { this.Remove(key); } } } internal void CheckReadOnly() { if(this._ReadOnly) { throw new Exception("Collection is read only"); } } public new void Clear() { this.CheckReadOnly(); base.Clear(); } public new void Add(String key, Object value) { this.CheckReadOnly(); base.Add(key, value); } public new void Remove(String key) { this.CheckReadOnly(); base.Remove(key); } public XmlSchema GetSchema() { return null; } public void ReadXml(XmlReader reader) { Boolean wasEmpty = reader.IsEmptyElement; reader.Read(); if(wasEmpty) { return; } while(reader.NodeType != XmlNodeType.EndElement) { if(reader.Name == "Item") { String key = reader.GetAttribute("Key"); Type type = Type.GetType(reader.GetAttribute("TypeName")); reader.Read(); if(type != null) { this.Add(key, new XmlSerializer(type).Deserialize(reader)); } else { reader.Skip(); } reader.ReadEndElement(); reader.MoveToContent(); } else { reader.ReadToFollowing("Item"); } reader.ReadEndElement(); } public void WriteXml(XmlWriter writer) { foreach(KeyValuePair&lt;String, Object&gt; item in this) { writer.WriteStartElement("Item"); writer.WriteAttributeString("Key", item.Key); writer.WriteAttributeString("TypeName", item.Value.GetType().AssemblyQualifiedName); new XmlSerializer(item.Value.GetType()).Serialize(writer, item.Value); writer.WriteEndElement(); } } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload