Is it possible to create a common Func <T> <T>
Is it possible to create a generic one Func<T><T>, as in Func, which takes a generic parameter and should return the type of this generic parameter?
In other words, can I do this?
Func<T> createNull = () => default(T);
Note that I have no context from the class or method, so I would like to do:
var user = createNull<User>();
Here is a bit more information on what I'm trying to do (note that the syntax is turned off because I donโt know how to do this and itโs not possible):
Func<TQuery, TResult><TQuery, TResult> query = q =>
(TResult) handlers[Tuple.Create(typeof(TQuery), typeof(TResult))](q);
where the handlers are declared as follows:
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 would use queryas follows:
User result = query<User, IdQuery>(new ById{Id = 1});
Customer result1 = query<Customer, ByName>(new ByName());
+4
2 answers