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

后端 未结 8 1807
名媛妹妹
名媛妹妹 2020-12-08 04:00

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

private void MyGenericClass ()
{
    if(T ... inherits or implements some class/i         


        
8条回答
  •  不思量自难忘°
    2020-12-08 04:16

    Although IsAssignableFrom is the best way as others have stated, if you only need to check if a class inherits from another, typeof(T).BaseType == typeof(SomeClass) does the job too.

提交回复
热议问题