using System; using System.Collections; using System.Collections.Generic; namespace Disco.Services.Expressions { public class LazyDictionary : IDictionary { private readonly Lazy> dictionary; public LazyDictionary(Func> dictionaryFactory) { if (dictionaryFactory == null) throw new ArgumentNullException(nameof(dictionaryFactory)); dictionary = new Lazy>(dictionaryFactory); } public string this[string key] { get => dictionary.Value.TryGetValue(key, out var value) ? value : null; set => throw new NotSupportedException(); } public ICollection Keys => dictionary.Value.Keys; public ICollection Values => dictionary.Value.Values; public int Count => dictionary.Value.Count; public bool IsReadOnly => true; public void Add(string key, string value) => throw new NotSupportedException(); public void Add(KeyValuePair item) => throw new NotSupportedException(); public void Clear() => throw new NotSupportedException(); public bool Contains(KeyValuePair item) => dictionary.Value.Contains(item); public bool ContainsKey(string key) => dictionary.Value.ContainsKey(key); public void CopyTo(KeyValuePair[] array, int arrayIndex) => throw new NotSupportedException(); public IEnumerator> GetEnumerator() => dictionary.Value.GetEnumerator(); public bool Remove(string key) => throw new NotSupportedException(); public bool Remove(KeyValuePair item) => throw new NotSupportedException(); public bool TryGetValue(string key, out string value) => dictionary.Value.TryGetValue(key, out value); IEnumerator IEnumerable.GetEnumerator() => dictionary.Value.GetEnumerator(); } }