You can't, basically. How would the compiler know what type you were interested in for any particular entry?
You can't even explain a relationship of Dictionary<Type, Action<T>>
where each dictionary entry has a key which is a type and an action which uses that type. Generics just doesn't support that relationship.
If you will know the kind when you try to use it, you can just make it a Dictionary<string, Delegate>
and cast the value when you fetch it. Alternatively, you could use Action<object>
and live with the boxing and cast you'll end up with.
Note that to use anonymous methods or lambda expressions with just Delegate
, you'll need to cast - or write a handy conversion method:
public static Delegate ConvertAction<T>(Action<T> action)
{
return action;
}
That way you can write:
Delegate dlg = ConvertAction((long x) => Console.WriteLine("Got {0}", x));
or in the dictionary context:
var dict = new Dictionary<string, Delegate>();
dict["Key1"] = ConvertAction((long x) => Console.WriteLine("Got {0}", x));
You'll still need to cast to the right type when you fetch the value out of the dictionary again though...
A different tack...
Instead of exposing a dictionary directly, you could encapsulate the dictionary in your own type, and have a generic Add
method:
public void Add<T>(string key, Action<T> action)
So it would still be a Dictionary<string, Delegate>
behind the scenes, but your type would make sure that it only contained values which were delegates taking a single argument.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…