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

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

问题描述

假设以下类型定义:

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

当只有 mangled 类型可用时,如何确定类型 Foo 是否实现了泛型接口 IBar?

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

推荐答案

通过使用 TcKs 的答案,也可以使用以下 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天全站免登陆