<PackageReference Include="NUnit" Version="3.0.0-beta-2" />

PropertyBag

A PropertyBag represents a collection of name value pairs that allows duplicate entries with the same key. Methods are provided for adding a new pair as well as for setting a key to a single value. All keys are strings but _values may be of any type. Null _values are not permitted, since a null entry represents the absence of the key.
using NUnit.Framework.Interfaces; using System.Collections; using System.Collections.Generic; namespace NUnit.Framework.Internal { public class PropertyBag : IPropertyBag, IXmlNodeBuilder { private Dictionary<string, IList> inner = new Dictionary<string, IList>(); public ICollection<string> Keys => inner.Keys; public IList this[string key] { get { if (!inner.TryGetValue(key, out IList value)) { value = new List<object>(); inner.Add(key, value); } return value; } set { inner[key] = value; } } public void Add(string key, object value) { if (!inner.TryGetValue(key, out IList value2)) { value2 = new List<object>(); inner.Add(key, value2); } value2.Add(value); } public void Set(string key, object value) { IList list = new List<object>(); list.Add(value); inner[key] = list; } public object Get(string key) { if (!inner.TryGetValue(key, out IList value) || value.Count <= 0) return null; return value[0]; } public bool ContainsKey(string key) { return inner.ContainsKey(key); } public XmlNode ToXml(bool recursive) { XmlNode parentNode = XmlNode.CreateTopLevelElement("dummy"); return AddToXml(parentNode, recursive); } public XmlNode AddToXml(XmlNode parentNode, bool recursive) { XmlNode xmlNode = parentNode.AddElement("properties"); foreach (string key in Keys) { foreach (object item in this[key]) { XmlNode xmlNode2 = xmlNode.AddElement("property"); xmlNode2.AddAttribute("name", key.ToString()); xmlNode2.AddAttribute("value", item.ToString()); } } return xmlNode; } } }