C# – dynamic return type of a function


How can I create a function that will have a dynamic return type based on the parameter type?


protected DynamicType Test(DynamicType type)

return ; 


Best Solution

You'd have to use generics for this. For example,

protected T Test<T>(T parameter)


In this example, the '<T>' tells the compiler that it represents the name of a type, but you don't know what that is in the context of creating this function. So you'd end up calling it like...

int foo;

int bar = Test<int>(foo);