#region Copyright & License Information /* * Copyright 2007-2020 The OpenRA Developers (see AUTHORS) * This file is part of OpenRA, which is free software. It is made * available to you under the terms of the GNU General Public License * as published by the Free Software Foundation, either version 3 of * the License, or (at your option) any later version. For more * information, see COPYING. */ #endregion using System; using System.Collections.Generic; namespace OpenRA { /// /// A minimal read only dictionary interface for .NET 4 /// /// /// .NET 4.5 has an implementation built-in, this code is not meant to /// duplicate it but provide a compatible interface that can be replaced /// when we switch to .NET 4.5 or higher. /// public interface IReadOnlyDictionary : IEnumerable> { int Count { get; } TValue this[TKey key] { get; } ICollection Keys { get; } ICollection Values { get; } bool ContainsKey(TKey key); bool TryGetValue(TKey key, out TValue value); } public static class ReadOnlyDictionary { public static IReadOnlyDictionary AsReadOnly(this IDictionary dict) { return dict as IReadOnlyDictionary ?? new ReadOnlyDictionary(dict); } } /// /// A minimal read only dictionary for .NET 4 implemented as a wrapper /// around an IDictionary. /// public class ReadOnlyDictionary : IReadOnlyDictionary { private readonly IDictionary dict; public ReadOnlyDictionary() : this(new Dictionary()) { } public ReadOnlyDictionary(IDictionary dict) { if (dict == null) throw new ArgumentNullException(nameof(dict)); this.dict = dict; } #region IReadOnlyDictionary implementation public bool ContainsKey(TKey key) { return dict.ContainsKey(key); } public bool TryGetValue(TKey key, out TValue value) { return dict.TryGetValue(key, out value); } public int Count => dict.Count; public TValue this[TKey key] => dict[key]; public ICollection Keys => dict.Keys; public ICollection Values => dict.Values; #endregion #region IEnumerable implementation public IEnumerator> GetEnumerator() { return dict.GetEnumerator(); } #endregion #region IEnumerable implementation System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return dict.GetEnumerator(); } #endregion } }