且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何确定一个类型实现用C#反射界面

更新时间:2022-12-04 19:51:02

您必须把我的头顶部的几个选择


  1. 的typeof(IMyInterface的).IsAssignableFrom(typeof运算(的MyType))


  2. typeof运算(的MyType).GetInterfaces()。包含(typeof运算(IMyInterface的))


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

You have a few choices off the top of my head

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

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