c# - 泛型类作为返回类型

标签 c# generics return-type idictionary

我有一个存储类,它使用泛型来保存不同的值。

public class Setting<T>
{
    ...
}

在另一个类中我想创建一个类似的方法

  public Setting<T> getSetting(string setting)
  {
        return (Setting<T>)settingDictionary[setting];
  }

设置字典在哪里

 private Dictionary<string, object> settingDictionary;

我收到错误:

The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)

有办法解决这个问题吗? 谢谢

最佳答案

您需要使该方法通用:

 public Setting<T> GetSetting<T>(string setting)
 {
   // ... Your code...

关于c# - 泛型类作为返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14485540/

相关文章:

C#高级排列场景

generics - 如何创建包含更高维度的代数矩阵的结构?

c# - 通用小数不能从 double 转换

c# - 我可以实现一个泛型类型并向它传递一个仅在运行时才知道的类型吗?

c# - AesCryptoServiceProvider GenerateIV() 的字节长度是多少?

c# - XMLWorker 异常 : Object reference not set to an instance of an object

c# - 通用模板错误

arrays - 如何指定任意嵌套长度的嵌套数组的返回类型?

java - 为什么 Java 5+ API 不利用协变返回类型?

c++ - 同时使用参数重载和返回类型重载