generic return type based on a Type paramater in C# -
i have class dictionary maps type
keys objects implement interface iwhatever
.
i wish create generic function gets type input , returns object mapped type in dictionary.
something :
public t get<t>(type type)
where t instance of type , type implements iwhatever
.
i don't want return object
or iwhatever
objects of given type. type of return objects can infered @ compile time. assume should possible.
i have managed in java:
public t get<t>(class<t extends iwhatever> type) { // implementation }
is there way achieve in c# ? if not, why not , alternatives suggest ?
i think want:
public t get<t>() t : iwhatever { ... }
for more information, see constraints on type parameters (c# programming guide).
this of course useful when client can provide type argument @ compile-time. internally, method may need lot of nastiness, e.g.:
return (t) mydict[typeof(t)];