Following scheme works fine dealing with strings/primitives. But when dealing with lists it gives type cast error in getObj(). The types used are dynamic and needs this generic use. Is there any better way to achieve it ?
public static Object obj;
static public T getObj<T>()
{
return (T)obj;
}
private static string getStr()
{
return "some string";
}
private static List<Object> getList()
{
List<Object> res = new List<object>();
Object o = "str1";
res.Add(o);
o = "str2";
res.Add(o);
return res;
}
public static void Main()
{
obj = getStr();
string s = getObj<string>();
obj = getList();
List<string> slist = getObj<List<string>>();
}