<PackageReference Include="NUnit" Version="4.3.1" />

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; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; namespace NUnit.Framework.Internal { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public class PropertyBag : IPropertyBag, IXmlNodeBuilder { private readonly 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) { Guard.ArgumentNotNull(value, "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) { Guard.ArgumentNotNull(key, "key"); Guard.ArgumentNotNull(value, "value"); IList list = new List<object>(); list.Add(value); _inner[key] = list; } [return: System.Runtime.CompilerServices.Nullable(2)] 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 bool TryGet(string key, [System.Runtime.CompilerServices.Nullable(2)] [NotNullWhen(true)] out IList values) { return _inner.TryGetValue(key, out values); } public TNode ToXml(bool recursive) { return AddToXml(new TNode("dummy"), recursive); } public TNode AddToXml(TNode parentNode, bool recursive) { TNode tNode = parentNode.AddElement("properties"); foreach (KeyValuePair<string, IList> item in _inner) { IList value = item.Value; int count = value.Count; for (int i = 0; i < count; i++) { TNode tNode2 = tNode.AddElement("property"); tNode2.AddAttribute("name", item.Key); tNode2.AddAttribute("value", value[i].ToString()); } } return tNode; } } }