如何确定是否一种类型的实现特定的通用的接口类型 [英] How to determine if a type implements a specific generic interface type

查看:197
本文介绍了如何确定是否一种类型的实现特定的通用的接口类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设以下类型定义:

public interface IFoo<T> : IBar<T> {}
public class Foo<T> : IFoo<T> {}

如何才能知道是否该类型实现了通用接口伊巴尔&LT; T&GT; 只有当错位类型可用?

How do I find out whether the type Foo implements the generic interface IBar<T> when only the mangled type is available?

推荐答案

通过使用答案从第三文化孩子也可以用下面的LINQ查询完成的:

By using the answer from TcKs it can also be done with the following LINQ query:

bool isBar = foo.GetType().GetInterfaces().Any(x =>
  x.IsGenericType &&
  x.GetGenericTypeDefinition() == typeof(IBar<>));

这篇关于如何确定是否一种类型的实现特定的通用的接口类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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