Skip to content

Instantly share code, notes, and snippets.

@ovatsus
Created September 19, 2011 01:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ovatsus/1225797 to your computer and use it in GitHub Desktop.
Save ovatsus/1225797 to your computer and use it in GitHub Desktop.
DoubleStaticStringDictionary
using System;
using System.Collections.Generic;
public static class DoubleStaticStringDictionary {
public static DoubleStaticStringDictionary<Type> Create<Type>(IEnumerable<KeyValuePair<string, Type>> dict, Func<string, Type> fallback, Func<Type, string> reverseFallback) {
return new DoubleStaticStringDictionary<Type>(dict, fallback, reverseFallback);
}
}
public class DoubleStaticStringDictionary<Type> : StaticStringDictionary<Type>, IDictionary<Type, string> {
private Func<Type, string> reverseFallback;
private IDictionary<Type, string> reverseDict;
public DoubleStaticStringDictionary(IEnumerable<KeyValuePair<string, Type>> dict, Func<string, Type> fallback, Func<Type, string> reverseFallback)
: base(dict, fallback) {
this.reverseFallback = reverseFallback;
reverseDict = new Dictionary<Type, string>();
foreach (KeyValuePair<string, Type> pair in dict) {
reverseDict.Add(pair.Value, pair.Key);
}
}
public void Add(Type key, string value) {
throw new InvalidOperationException();
}
public bool ContainsKey(Type key) {
throw new InvalidOperationException();
}
public new ICollection<Type> Keys {
get { throw new InvalidOperationException(); }
}
public bool Remove(Type key) {
throw new InvalidOperationException();
}
public bool TryGetValue(Type key, out string value) {
throw new InvalidOperationException();
}
public new ICollection<string> Values {
get { throw new InvalidOperationException(); }
}
public string this[Type key] {
get {
string result;
if (reverseDict.TryGetValue(key, out result)) {
return result;
} else {
return reverseFallback(key);
}
}
set { throw new InvalidOperationException(); }
}
public void Add(KeyValuePair<Type, string> item) {
throw new InvalidOperationException();
}
public bool Contains(KeyValuePair<Type, string> item) {
throw new InvalidOperationException();
}
public void CopyTo(KeyValuePair<Type, string>[] array, int arrayIndex) {
throw new InvalidOperationException();
}
public bool Remove(KeyValuePair<Type, string> item) {
throw new InvalidOperationException();
}
public new IEnumerator<KeyValuePair<Type, string>> GetEnumerator() {
throw new InvalidOperationException();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment