user1229895 user1229895 - 1 year ago 72
C# Question

Check if 'T' inherits or implements a class/interface

Is there a way to test if T inherits/implements a class/interface?

private void MyGenericClass<T> ()
if(T ... inherits or implements some class/interface

Answer Source

There is a Method called Type.IsAssignableFrom().

It is used like this:


If you are targeting .NET Core, the method has moved to TypeInfo:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download