2016-10-22 20 views
0

我该如何实现这样的功能。我不想要返回铸造值的方法,我想用this[]这可能吗?在字典中通用此索引属性

public class DynamicDictionary : IDictionary<string, object> 
{ 
    public T this<T>[string key] where T : class 
    { 
     get 
     { 
      object value = null; 
      TryGetValue(key, out value); 
      return (T)value; 
     } 
    } 

    // ... 
} 

回答

2

不,你不能。索引器 - 以及一般属性 - 在C#中不能是通用的。您必须改用方法,或者只是在调用代码中投射。