Hey,
I'm trying to write a method like this:
public static T Test<T>()
{
if (typeof(T)==typeof(string))
return "1241";
// do something else
}
but I can't seem to figure out how to pull it off. I want to return values depending on the type of T that the method was invoked with. I need to return strings, int's, custom classes, List etc.
The actual usecase is some custom serialization code where it is essential that the deserializing code knows the type of the object it should produce.
Clarification: the example above gives the following error: Cannot convert string to type T
The ideal solution would work on value types and reference types, and would not include a dummy parameter for overload resolution.
I'm starting to doubt if that ideal solution exists though.
Thanks, Lucas