Check if class(T) inherits an interface(T)

The very first thing which comes to our mind is is operator like

bool IsDerived = a class is Interface

Well, that is not correct. why ? because

is operator is used to check whether the run-time type of an object
is compatible with a given type.

An expression where the use of is conforms to the syntax, evaluates to true, if both of the following conditions are met:

  • expression is not null.
  • expression can be cast to type. That is, a cast expression of the form
    (type)(expression) will complete without throwing an exception. For
    more information, see 7.6.6 Cast
    expressions.
  • Solution is to use:

    bool IsDerived = typeof(ISomeInterface).IsAssignableFrom(typeof(T));

    References

  • Does a Type Implement an Interface?
  • is operator
  • Advertisements