如何确定一个类型是否实现了特定的泛型接口类型

278

假设以下类型定义:

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

当只有编码后的类型可用时,如何确定类型 Foo 是否实现了泛型接口 IBar<T>

14个回答

479

通过使用TcKs的答案,也可以使用以下LINQ查询来完成:

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

1
这是一个非常优雅的解决方案!我在 Stack Overflow 上看到的其他解决方案都使用 foreach 循环或更长的 LINQ 查询。但请记住,要使用此解决方案,您需要拥有 .NET Framework 3.5。 - Daniel T.
7
我建议您将此方法转换为扩展方法,就像http://bit.ly/ccza8B一样 - 这将使代码变得更加简洁明了! - Brad Heller
2
根据您的需求,您可能需要在返回的接口上进行递归。 - Sebastian Good
6
我认为这应该在 .net 中更好地实现...作为核心...像 member.Implements(IBar) 或 CustomType.Implements(IBar),或者更好的是使用关键字 "is"....我正在探索 c#,但我对 .net 目前感到非常失望... - Sofija
4
好的,我会尽力为您提供准确和简明易懂的翻译。以下是需要翻译的内容:minor addition: if IBar has multiple generic types, you need to indicate this like: typeof(IBar<,,,>) with commas acting like placeholders小改动:如果 IBar 有多个泛型类型,你需要这样表示:typeof(IBar<,,,>),其中逗号充当占位符。 - Rob Von Nesselrode
我应该补充说明,TypeSupport 使用 type.Implements(typeof(IBar<>)) 非常干净地添加了这个功能,并覆盖了所有情况。 - Michael Brown

36
您需要沿着继承树向上查找每个类的所有接口,并将 typeof(IBar<>) 与调用 Type.GetGenericTypeDefinition 的结果进行比较,如果 接口是泛型的。这一切都有点麻烦,确实。
详情和代码,请参见此答案这些答案

为什么不直接将其转换为IBar<SomeClass>并检查是否为空呢?(当然,我是指使用'as'进行转换) - Pablo Retyk
5
T是未知的,不能转换为特定类型。 - sduplooy
@sduplooy:也许我漏掉了什么,T 怎么会是未知的?它应该编译 public class Foo : IFoo<T> {}。 - Pablo Retyk

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

var implementedInterfaces = typeof( Foo ).GetInterfaces();
foreach( var interfaceType in implementedInterfaces ) {
    if ( false == interfaceType.IsGeneric ) { continue; }
    var genericType = interfaceType.GetGenericTypeDefinition();
    if ( genericType == typeof( IFoo<> ) ) {
        // do something !
        break;
    }
}

2
由于 typeof(Foo) 返回描述 Foo 的 System.Type 对象,因此 GetType() 调用将始终返回 System.Type 的类型。您应该更改为 typeof(Foo).GetInterfaces()。 - Michael Meadows

6
作为一个辅助方法扩展
public static bool Implements<I>(this Type type, I @interface) where I : class
{
    if(((@interface as Type)==null) || !(@interface as Type).IsInterface)
        throw new ArgumentException("Only interfaces can be 'implemented'.");

    return (@interface as Type).IsAssignableFrom(type);
}

示例用法:

var testObject = new Dictionary<int, object>();
result = testObject.GetType().Implements(typeof(IDictionary<int, object>)); // true!

2
"IsAssignableFrom" 正是我正在寻找的 - 谢谢 - Jesper
27
这不符合提问者不知道泛型类型参数的要求。从您的示例中,testObject.GetType().Implements(typeof(IDictionary<,>)); 将返回 false。 - ctusch
1
@ctusch 那么,有任何解决办法吗? - Tohid

6

我正在使用稍微简化版本的@GenericProgrammers扩展方法:

public static bool Implements<TInterface>(this Type type) where TInterface : class {
    var interfaceType = typeof(TInterface);

    if (!interfaceType.IsInterface)
        throw new InvalidOperationException("Only interfaces can be implemented.");

    return (interfaceType.IsAssignableFrom(type));
}

使用方法:

    if (!featureType.Implements<IFeature>())
        throw new InvalidCastException();

7
仍然无法满足原问题中要求的通用接口。 - nathanchere

5

如果您需要一个支持泛型基类型和接口的扩展方法,我对sduplooy的答案进行了扩展:

    public static bool InheritsFrom(this Type t1, Type t2)
    {
        if (null == t1 || null == t2)
            return false;

        if (null != t1.BaseType &&
            t1.BaseType.IsGenericType &&
            t1.BaseType.GetGenericTypeDefinition() == t2)
        {
            return true;
        }

        if (InheritsFrom(t1.BaseType, t2))
            return true;

        return
            (t2.IsAssignableFrom(t1) && t1 != t2)
            ||
            t1.GetInterfaces().Any(x =>
              x.IsGenericType &&
              x.GetGenericTypeDefinition() == t2);
    }

1
这个扩展简直完美无缺! - Stephen Lautier

5

要完全解决类型系统问题,我认为您需要处理递归,例如: IList<T>ICollection<T>IEnumerable<T>,否则您将不知道 IList<int> 最终实现了 IEnumerable<>

    /// <summary>Determines whether a type, like IList&lt;int&gt;, implements an open generic interface, like
    /// IEnumerable&lt;&gt;. Note that this only checks against *interfaces*.</summary>
    /// <param name="candidateType">The type to check.</param>
    /// <param name="openGenericInterfaceType">The open generic type which it may impelement</param>
    /// <returns>Whether the candidate type implements the open interface.</returns>
    public static bool ImplementsOpenGenericInterface(this Type candidateType, Type openGenericInterfaceType)
    {
        Contract.Requires(candidateType != null);
        Contract.Requires(openGenericInterfaceType != null);

        return
            candidateType.Equals(openGenericInterfaceType) ||
            (candidateType.IsGenericType && candidateType.GetGenericTypeDefinition().Equals(openGenericInterfaceType)) ||
            candidateType.GetInterfaces().Any(i => i.IsGenericType && i.ImplementsOpenGenericInterface(openGenericInterfaceType));

    }

3

您需要针对泛型接口的构造类型进行检查。

您需要执行以下操作:

foo is IBar<String>

因为 IBar<String> 代表了这个构造类型。你必须这样做的原因是,如果在你的检查中 T 没有定义,编译器就不知道你是指 IBar<Int32> 还是 IBar<SomethingElse>


3

首先 public class Foo : IFoo<T> {} 无法编译,因为你需要指定一个类而不是 T。但是假设你像这样做:public class Foo : IFoo<SomeClass> {}

然后,如果你执行:

Foo f = new Foo();
IBar<SomeClass> b = f as IBar<SomeClass>;

if(b != null)  //derives from IBar<>
    Blabla();

2
var genericType = typeof(ITest<>);
Console.WriteLine(typeof(Test).GetInterfaces().Any(x => x.GetGenericTypeDefinition().Equals(genericType))); // prints: "True"

interface ITest<T> { };

class Test : ITest<string> { }

这对我很有用。

这应该是被接受的答案。请注意,在调用 GetGenericTypeDefinition() 之前检查类型是否为泛型通常是一个好主意,否则可能会引发异常。 - Christian Klemm

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接