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
}
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There is a Method called Type.IsAssignableFrom().
To check if
Tinherits/implementsEmployee:If you are targeting .NET Core, the method has moved to TypeInfo:
Note that if you want to constrain your type
Tto 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.