如何根据泛型T的类型返回值

Bla*_*man 3 c# asp.net generics

我有一个方法,如:

public T Get<T>(string key)
{

}
Run Code Online (Sandbox Code Playgroud)

现在说如果类型是字符串,我想返回"hello",如果是int类型则返回110011.

我怎样才能做到这一点?

typeof(T)似乎不起作用.

理想情况下,我想做一个switch语句,并根据泛型的类型返回一些东西(string/int/long/etc).

这可能吗?

Jar*_*Par 10

以下应该有效

public T Get<T>(string key) { 
   object value = null;
   if ( typeof(T) == typeof(int) ) { 
     value = 11011;
   } else if ( typeof(T) == typeof(string) ) { 
     value = "hello";
   }
   return (T)value;
}
Run Code Online (Sandbox Code Playgroud)