public static class MyDict_String { public static string Value { get; set; } } public static class MyDict_Int32 { public static int Value { get; set; } } MyDict_String.Value = MyDict_Int32.Value.ToString();
public class MyTypedDict { private Dictionary Values = new Dictionary(); public T Get() { object untypedValue; if (Values.TryGetValue(typeof(T), out untypedValue)) return (T)untypedValue; return default(T); } public void Set(T value) { Values[typeof(T)] = value; } }
public class TypeDict { public T Get() { return MyDict.Values[this]; } public void Set(T value) { MyDict.Values[this] = value; } private static class MyDict { public static Dictionary Values { get; private set; } static MyDict() { Values = new Dictionary(); } } }
现在我们可以像这样使用TypeDict:
void X() { var a = new TypeDict(); var b = new TypeDict(); a.Set(1); a.Set(3.14); a.Set("Hello, world!"); //Note that type inference allows us to omit the type argument b.Set(10); b.Set(31.4); b.Set("Hello, world, times ten!"); Console.WriteLine(a.Get()); Console.WriteLine(a.Get()); Console.WriteLine(a.Get()); Console.WriteLine(); Console.WriteLine(b.Get()); Console.WriteLine(b.Get()); Console.WriteLine(b.Get()); }
一个更复杂的版本。 不知道它是否更接近:
定义通用字典:
public class MyDictionary { Dictionary dict; public MyDictionary() { dict = new Dictionary(); } public T this[string name] { get { if (dict.ContainsKey(name)) return dict[name]; else return default(T);//or throw } set { dict[name] = value; } } }
然后是存储这些词典的存储库:
public class MyRepository { List
最后你可以使用这个存储库:
MyRepository repo = new MyRepository(); repo.Add("A", 1); repo.Add("B", 1); int i = repo.GetValue("A") + repo.GetValue("B");
在这个例子中,还有MyDictionary装箱object 。
另一方面,如果您正在使用某些类型,则可能根本不使用该存储库类。 但是使用单独的词典。
MyDictionary intDict = new MyDictionary(); intDict["A"] = 1; intDict["B"] = 2; int i = intDict["A"] + intDict["B"];
public class TypeDictionary { static int _maxId = 0; int _id; static class Store{ internal static List Values = new List(); } public TypeDictionary() { _id = _maxId++; } public T GetValue() { return Store.Values[_id]; } public void SetValue(T value) { while(Store.Values.Count < _id) { Store.Values.Add(default(T)); } Store.Values[_id] = value; } }
此代码可以使用如下:
var dict1 = new TypeDictionary(); dict1.SetValue("my string"); string result = dict1.GetValue();
此解决方案的问题在于存储库不稀疏导致的内存使用情况。 这也使第一次设置值更加昂贵。
试试这个:
public class MyDictionary { List values; public MyDictionary() { values = new List(); } public T GetValue() { return values.OfType().FirstOrDefault(); } public bool Add(T value) { if (values.OfType().Any()) return false; else { values.Add(value); return true; } } }
并使用它:
var md = new MyDictionary(); md.Add("!!!"); string s = md.GetValue();
public class TypeDict { public T Get() where T : class { T value; InnerDict.Values.TryGetValue(this, out value); return value; } public void Set(T value) where T : class { var cwt = InnerDict.Values; // lock+remove+add https://github.com/dotnet/coreclr/issues/4545 lock (cwt) { cwt.Remove(this); cwt.Add(this, value); } } private static class InnerDict where T : class { public static ConditionalWeakTable Values { get; private set; } static InnerDict() { Values = new ConditionalWeakTable(); } } }