I have a storage class that uses generics to hold different values.
public class Setting<T>
{
...
}
In another class I want to make a method like
public Setting<T> getSetting(string setting)
{
return (Setting<T>)settingDictionary[setting];
}
Where settingDictionary is
private Dictionary<string, object> settingDictionary;
I get error:
The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?)
Is there a way to solve this? thanks
You need to make the method generic:
public Setting<T> GetSetting<T>(string setting)
{
// ... Your code...
Your Setting<T>
class doesn't implement Dictionary<string, object>
so you should make your method generic type
like GetSetting<T>
public Setting<T> GetSetting<T>(string setting)
{
}
Here is a DEMO
.
好吧,编译器不知道“ T
”是什么,您可以将其定义为通用方法,如下所示:
public Setting<T> getSetting<T>(string setting)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.