Is there a way to test if T inherits/implements a class/interface?
private void MyGenericClass ()
{
if(T ... inherits or implements some class/i
The correct syntax is
typeof(Employee).IsAssignableFrom(typeof(T))
Return Value:
trueifcand the currentTyperepresent the same type, or if the currentTypeis in the inheritance hierarchy ofc, or if the currentTypeis aninterfacethatcimplements, or ifcis a generic type parameter and the currentTyperepresents one of the constraints ofc, or ifcrepresents a value type and the currentTyperepresentsNullable(Nullable(Of c)in Visual Basic).falseif none of these conditions aretrue, or ifcisnull.
source
If Employee IsAssignableFrom T then T inherits from Employee.
The usage
typeof(T).IsAssignableFrom(typeof(Employee))
returns true only when either
T and Employee represent the same type; or,Employee inherits from T.This may be intended usage in some case, but for the original question (and the more common usage), to determine when T inherits or implements some class/interface, use:
typeof(Employee).IsAssignableFrom(typeof(T))