假设有以下类型定义:
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类型是可用的?
当前回答
您必须遍历继承树并找到树中每个类的所有接口,并将typeof(IBar<>)与调用Type的结果进行比较。GetGenericTypeDefinition如果接口是泛型的。当然,这一切都有点痛苦。
更多信息和代码请参见这个答案和这些答案。
其他回答
您必须检查泛型接口的构造类型。
你必须这样做:
foo is IBar<String>
因为IBar<String>表示构造的类型。你必须这样做的原因是,如果T在检查中是未定义的,编译器不知道你是指IBar<Int32>还是IBar<SomethingElse>。
您必须遍历继承树并找到树中每个类的所有接口,并将typeof(IBar<>)与调用Type的结果进行比较。GetGenericTypeDefinition如果接口是泛型的。当然,这一切都有点痛苦。
更多信息和代码请参见这个答案和这些答案。
以下内容应该没有任何问题:
bool implementsGeneric = (anObject.Implements("IBar`1") != null);
如果您想为IBar查询提供一个特定的泛型类型参数,您可以捕获AmbiguousMatchException。
作为辅助方法扩展
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!
如果你想要一个支持泛型基类型和接口的扩展方法,我扩展了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);
}