using System; using System.Collections; using System.Collections.Generic; namespace AspClassic.Scripting.Utils; [Serializable] internal sealed class ReadOnlyDictionary : IDictionary, ICollection>, IEnumerable>, IEnumerable { private sealed class ReadOnlyWrapper : ICollection, IEnumerable, IEnumerable { private readonly ICollection _collection; public int Count => _collection.Count; public bool IsReadOnly => true; internal ReadOnlyWrapper(ICollection collection) { _collection = collection; } public void Add(T item) { throw new NotSupportedException("Collection is read-only."); } public void Clear() { throw new NotSupportedException("Collection is read-only."); } public bool Contains(T item) { return _collection.Contains(item); } public void CopyTo(T[] array, int arrayIndex) { _collection.CopyTo(array, arrayIndex); } public bool Remove(T item) { throw new NotSupportedException("Collection is read-only."); } public IEnumerator GetEnumerator() { return _collection.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return _collection.GetEnumerator(); } } private readonly IDictionary _dict; public ICollection Keys { get { ICollection keys = _dict.Keys; if (!keys.IsReadOnly) { return new ReadOnlyWrapper(keys); } return keys; } } public ICollection Values { get { ICollection values = _dict.Values; if (!values.IsReadOnly) { return new ReadOnlyWrapper(values); } return values; } } public TValue this[TKey key] => _dict[key]; TValue IDictionary.this[TKey key] { get { return _dict[key]; } set { throw new NotSupportedException("Collection is read-only."); } } public int Count => _dict.Count; public bool IsReadOnly => true; public ReadOnlyDictionary(IDictionary dict) { ReadOnlyDictionary readOnlyDictionary = dict as ReadOnlyDictionary; _dict = ((readOnlyDictionary != null) ? readOnlyDictionary._dict : dict); } public bool ContainsKey(TKey key) { return _dict.ContainsKey(key); } public bool TryGetValue(TKey key, out TValue value) { return _dict.TryGetValue(key, out value); } void IDictionary.Add(TKey key, TValue value) { throw new NotSupportedException("Collection is read-only."); } bool IDictionary.Remove(TKey key) { throw new NotSupportedException("Collection is read-only."); } public bool Contains(KeyValuePair item) { return _dict.Contains(item); } public void CopyTo(KeyValuePair[] array, int arrayIndex) { _dict.CopyTo(array, arrayIndex); } void ICollection>.Add(KeyValuePair item) { throw new NotSupportedException("Collection is read-only."); } void ICollection>.Clear() { throw new NotSupportedException("Collection is read-only."); } bool ICollection>.Remove(KeyValuePair item) { throw new NotSupportedException("Collection is read-only."); } public IEnumerator> GetEnumerator() { return _dict.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return _dict.GetEnumerator(); } }