c# - Test whether an object implements a generic interface for any generic type -


i want test object see if implements idictionary<tkey,tvalue> don't care tkey , tvalue are.

i can test if concrete instance of framework dictionary<,> this:

bool isdict = type.isgenerictype &&      (typeof(dictionary<,>).isassignablefrom(type.getgenerictypedefinition()); 

but can't think of way test implements idictionary<,>. technique doesn't work interface; isassignablefrom return false if test against generic base type idictionary<,>, seems odd since works concrete type.

normally use is test if implements interface, of course works if want test specific generic interface. or test common ancestral interface, unlike other generic data structures such ilist<> , icollection<>, there no unique non-generic interface generic idictionary<tkey,tvalue> inherits.

how like

return type.getinterfaces()            .where(t => t.isgenerictype)            .select(t => t.getgenerictypedefinition())            .any(t => t.equals(typeof(idictionary<,>))); 

which i'm sure can generalize generic type definition.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -