How to check if a generic type definition inherits from another generic type definition?

The problem is that GetInterfaces returns closed types so you need to open them using GetGenericTypeDefinition : public static bool ImplementsOpenInterface(Type type, Type openInterfaceType) { Contract. Requires(type! = null); Contract.

Requires(openInterfaceType! = null); Contract. Requires(openInterfaceType.

IsGenericTypeDefinition); Type interfaces = type.GetInterfaces(); if (interfaces == null) { return false; } return interfaces . Where(x => x. IsGenericType) .

Select(x => x. GetGenericTypeDefinition()) . Any(x => x == openInterfaceType); }.

The problem is that GetInterfaces returns closed types so you need to open them using GetGenericTypeDefinition: public static bool ImplementsOpenInterface(Type type, Type openInterfaceType) { Contract. Requires(type! = null); Contract.

Requires(openInterfaceType! = null); Contract. Requires(openInterfaceType.

IsGenericTypeDefinition); Type interfaces = type.GetInterfaces(); if (interfaces == null) { return false; } return interfaces . Where(x => x. IsGenericType) .

Select(x => x. GetGenericTypeDefinition()) . Any(x => x == openInterfaceType); }.

1 Cool. That works. Thanks.

– Anne Jan 6 at 19:57.

Change your method with this and it will work: private static bool OpenGenericTypeImplementsOpenGenericInterface( Type derivedType, Type interfaceType) { return derivedType. GetInterface(interfaceType. Name)!

= null; }.

Note that interfaceType. Name is IService1` – SLaks Jan 6 at 19:40 Ok, what are you trying to say? – HABJAN Jan 6 at 19:41 Just that the name is not what you might expect.

Your answer is correct. – SLaks Jan 6 at 19:42 It is generic interface, the name should be correct. – HABJAN Jan 6 at 19:45.

GetInterfaces() will return a closed Type object with the generic parameter that it implements the interface with. Instead, use LINQ: return derivedType.GetInterfaces(). Any(i => I == interfaceType || (i.

ContainsGenericParameters && i. GetGenericTypeDefinition() == interfaceType)) This code checks whether any of the interfaces that it implements is a parameterized version of your interface.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions