Can I see if an IDictionary object inherits without type type parameters?

I am testing the object as follows:

if (item is IDictionary<object, object>) 

But this does not match all the other combinations of types <sting, object>, <int, string> , etc.

I just want to know if it implemented an interface no matter what generic types it uses.

I found an example that said it is possible to do something like:

 dictionary.GetType().GetInterfaces().Any(x => x.GetGenericTypeDefinition == typeof(IDictionary<>)); 

But I still need to specify a type signature or invalid.

Is it possible to make an operator that checks the interface without specifying a type?

+5
source share
1 answer

You're close, you just need to fix the syntax:

 dictionary.GetType().GetInterfaces().Any(x => x.GetGenericTypeDefinition() == typeof(IDictionary<,>)) 

Note the () after GetGenericTypeDefinition and the comma inside <> .

+7
source

All Articles