Alex Rivera | Logout

How to determine if a type implements an interface with C# reflection

Asked 2011-02-10T21:49:47.320
741

Does reflection in C# offer a way to determine if some given System.Type type models some interface?

public interface IMyInterface {}

public class MyType : IMyInterface {}

// should yield 'true'
typeof(MyType)./* ????? */MODELS_INTERFACE(IMyInterface);
Edit
Report

1 Answer

38
typeof(IMyInterface).IsAssignableFrom(someclass.GetType());

or

typeof(IMyInterface).IsAssignableFrom(typeof(MyType));
answered 2011-02-10T21:53:02.970

Your Answer