我有一個方法,如重構使用泛型
bool TryGetValue(string key, out string value);
bool TryGetValue(string key, out int value);
bool TryGetValue(string key, out double value);
bool TryGetValue(string key, out DateTime value);
// only value types allowed
//with the implementation based on dictionary<string, object>
bool TryGetValue(string key, out string value)
{
object rc;
if (dict.TryGetValue(key, out rc))
{
value = rc.ToString();
return true;
}
value = null;
return false;
}
看起來像仿製藥完美的情況下,
bool TryGetValue<T>(string key, out T value) where T: ValueType;
除了不能工作了FUNC實施,任何一個簡單的界面?
更新 - 以下不編譯,我想避免創建多個TryGet ... funcs!
bool TryGetValue<T>(string key, out T value)
{
return dict.TryGetValue(key, out value) ;
}
看到更新以上 – Kumar 2010-02-04 02:20:00
@Kumar - 更新了答案...希望這就是你以後,如果不是請讓我知道你錯過了什麼。 – 2010-02-04 02:38:53