我怎么能做到这样呢?我不想要返回转换值的方法,我想使用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;
}
}
// ...
}
不行。索引器——以及一般的属性——在c#中不能是通用的。您必须使用一个方法,或者只是在调用代码中强制转换。