假设有以下类型定义:
public interface IFoo<T> : IBar<T> {}
public class Foo<T> : IFoo<T> {}
我如何找出类型Foo是否实现了通用接口IBar<T>时,只有manged类型是可用的?
假设有以下类型定义:
public interface IFoo<T> : IBar<T> {}
public class Foo<T> : IFoo<T> {}
我如何找出类型Foo是否实现了通用接口IBar<T>时,只有manged类型是可用的?
当前回答
首先,公共类Foo: IFoo<T>{}不能编译,因为你需要指定一个类而不是T,但假设你做的事情类似于公共类Foo: IFoo<SomeClass> {}
如果你这样做了
Foo f = new Foo();
IBar<SomeClass> b = f as IBar<SomeClass>;
if(b != null) //derives from IBar<>
Blabla();
其他回答
作为辅助方法扩展
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!
我使用一个稍微简单的版本的@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 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));
}
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;
}
}
以下内容应该没有任何问题:
bool implementsGeneric = (anObject.Implements("IBar`1") != null);
如果您想为IBar查询提供一个特定的泛型类型参数,您可以捕获AmbiguousMatchException。