Is it possible to create a generic Func<T><T>
, as in a Func that accepts a generic parameter and needs to return the type of that generic parameter?
In other words, can I do this?
Func<T> createNull = () => default(T);
Note that I don't have any context from a containing class or method, so I'd want to do:
var user = createNull<User>();
Here's a bit more info about what I'm trying to do.(note that the syntax is off, because I don't know how to do it nor whether it's possible):
Func<TQuery, TResult><TQuery, TResult> query = q =>
(TResult) handlers[Tuple.Create(typeof(TQuery), typeof(TResult))](q);
where handlers is declared as following:
var handlers = new Dictionary<Tuple<Type, Type>, Func<TQuery, TResult><TQuery, TResult>();
// examples
handlers.Add(Tuple.Create(typeof(ById), typeof(User)),
idQuery => new User());
handlers.Add(Tuple.Create(typeof(ByName), typeof(Customer)),
otherQuery => new Customer());
Then I'd like to use query
like this:
User result = query<User, IdQuery>(new ById{Id = 1});
Customer result1 = query<Customer, ByName>(new ByName());