Has anybody heard of a "type dictionary" that uses types as keys and supports inheritance?
In my application I'd like to have a dictionary from types to functions, sort of like this:
Dictionary<Type, Func<object, object>> Transformers;
The idea is that it would be used to transform an object in some fashion based on its type:
// Transform an object 'obj'
object result = Transformers[obj.GetType()](obj)
An ordinary dictionary has the disadvantage that the type must match exactly. So if I've written a transformer for IList<T>, there's no use putting it in the Transformers dictionary because no object has the type IList<T> (only T[], List<T>, etc.) In other words, if obj is a List<T>, the transformer for IList<T> will not be found by a lookup in an ordinary dictionary.
Assuming there is no such thing as a TypeDictionary<TValue>, I might consider writing one if it isn't too hard. Any ideas how it could be accomplished?