I have a general method:
Func<IEnumerable<T>, bool> CreateFunction<T>()
where T can be any number of different types. This method uses a bunch of material using reflection, and if T is an IDictionary , regardless of the TKey and TValue I need to execute certain dictionary code.
Thus, the method could be called:
var f = CreateFunction<string>(); var f0 = CreateFunction<SomePocoType>(); var f1 = CreateFunction<IDictionary<string,object>>(); var f2 = CreateFunction<Dictionary<string,object>>(); var f3 = CreateFunction<SomeDerivedDictionaryType<string,object>>();
and etc.
Clarification in @Andy's answer
Ultimately, I want to know if T inherits from / implements IDictionary , even if T itself is a Dictionary or some other type that derives from this interface.
if(typeof(T) == typeof(IDictionary<,>)
does not work because T is a generic type, not a definition of a generic type.
And without knowing TKey and TValue (which are unknown at compile time), I canβt compare with any particular type that I would know before runtime.
The only thing I came up with was to look at the type name or check its method with reflection, looking for methods that would make me believe that this is a dictionary (i.e. look for ContainsKey and get_Item ).
Is there an easy way to make such a definition?