Yippie-Ki-Yay Yippie-Ki-Yay - 25 days ago 12
C# Question

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

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);

Answer

You have a few choices off the top of my head

  1. typeof(IMyInterface).IsAssignableFrom(typeof(MyType))

  2. typeof(MyType).GetInterfaces().Contains(typeof(IMyInterface))

Comments