The argument of the first type of Func is the input, the second is the output: Func<in T, out TResult> - so you need Func<string, T> .
(The MSDN link here uses Func<string, string> fair bit, which is annoying.)
In addition, the dictionary cannot use an argument of type T as different for each element of the dictionary. Rather, use the superclass Func<T, TResult> , which is equal to Delegate .
This should work:
Dictionary<Type, Delegate> dictionary = new Dictionary<Type, Delegate>(); public void Map<T>(Func<string, T> mapper) { dictionary[typeof(T)] = mapper; } public T Call<T>(string value) { var func = dictionary[typeof(T)] as Func<string, T>; return func.Invoke(value); }
source share