Check if ‘T’ inherits or implements a class/interface

There is a Method called Type.IsAssignableFrom().

To check if T inherits/implements Employee:

typeof(Employee).IsAssignableFrom(typeof(T));

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

typeof(Employee).GetTypeInfo().IsAssignableFrom(typeof(T).Ge‌​tTypeInfo())

Note that if you want to constrain your type T to implement some interface or inherit from some class, you should go for @snajahi’s answer, which uses compile-time checks for that and genereally resembles a better approach to this problem.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)