如何确定一个types是否实现了一个特定的通用接口types

假定以下types定义:

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

如何才能findtypesFoo是否实现通用接口IBar<T>只有损坏types可用?

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

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

你必须通过inheritance树,find树中每个类的所有接口,并且比较typeof(IBar<>)和调用Type.GetGenericTypeDefinition 如果接口是通用的typeof(IBar<>)的结果。 当然,这一切都有点痛苦。

看到这个答案 , 这些更多的信息和代码。

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

作为辅助方法扩展

 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! 

你必须检查构造types的通用接口。

你将不得不做这样的事情:

 foo is IBar<String> 

因为IBar<String>表示构造的types。 你必须这样做的原因是因为如果你的支票中没有定义T ,编译器不知道你的意思是IBar<Int32>还是IBar<SomethingElse>

我正在使用@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(); 

首先, public class Foo : IFoo<T> {}不能编译,因为你需要指定一个类而不是T,但是假设你做了类似于public class Foo : IFoo<SomeClass> {}事情public class Foo : IFoo<SomeClass> {}

那么如果你这样做

 Foo f = new Foo(); IBar<SomeClass> b = f as IBar<SomeClass>; if(b != null) //derives from IBar<> Blabla(); 

为了完全处理types系统,我认为你需要处理recursion,例如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)); } 

如果你想要一个支持generics基types和接口的扩展方法,我已经扩展了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); } 

检查types是否inheritance或实现genericstypes的方法:

  public static bool IsTheGenericType(this Type candidateType, Type genericType) { return candidateType != null && genericType != null && (candidateType.IsGenericType && candidateType.GetGenericTypeDefinition() == genericType || candidateType.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == genericType) || candidateType.BaseType != null && candidateType.BaseType.IsTheGenericType(genericType)); } 

以下不应该有任何错误:

 bool implementsGeneric = (anObject.Implements("IBar`1") != null); 

对于额外的功劳,如果你想用你的IBar查询提供一个特定的genericstypes参数,你可以捕获AmbiguousMatchException。