如何确定一个类型实现用C#反射界面 [英] How to determine if a type implements an interface with C# reflection

查看:208
本文介绍了如何确定一个类型实现用C#反射界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

确实反射 C#提供了一种方法来确定某个特定的的System.Type 类模型在某个接口?

 公共接口IMyInterface的{}公共类的MyType:IMyInterface的{}//应该产生真
typeof运算(的MyType)./* ????? * / MODELS_INTERFACE(IMyInterface的);


解决方案

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


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

这篇关于如何确定一个类型实现用C#反射界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆