如何确定类型是否实现特定的通用接口类型

sdu*_*ooy 211 .net c# reflection

假设以下类型定义:

public interface IFoo<T> : IBar<T> {}
public class Foo<T> : IFoo<T> {}
Run Code Online (Sandbox Code Playgroud)

当只有受损的类型可用时,如何确定类型是否Foo实现了通用接口IBar<T>

sdu*_*ooy 370

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

bool isBar = foo.GetType().GetInterfaces().Any(x =>
  x.IsGenericType &&
  x.GetGenericTypeDefinition() == typeof(IBar<>));
Run Code Online (Sandbox Code Playgroud)

  • 我建议你把它作为一个扩展方法来http://bit.ly/ccza8B - 这将很好地清理它! (7认同)
  • 我说这应该在.net中实现得更好......作为核心......就像member.Implements(IBar)或CustomType.Implements(IBar),甚至更好,使用关键字"is".. ..我正在探索c#,我现在对.net感到有点失望...... (4认同)
  • 根据您的需求,您可能会发现需要重复返回的接口。 (2认同)
  • 较小的补充:如果IBar具有多个泛型类型,则需要像这样表示:`typeof(IBar &lt;,,,&gt;)`,逗号的作用类似于占位符 (2认同)

Jon*_*eet 32

你必须通过继承树上去,找到树中的每个类的所有接口,并比较typeof(IBar<>)与调用的结果Type.GetGenericTypeDefinition ,如果该接口是通用的.当然,这有点痛苦.

有关更多信息和代码,请参阅此答案这些答案.

  • T未知,无法转换为特定类型. (4认同)

TcK*_*cKs 23

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 由于typeof(Foo)返回一个System.Type对象(描述Foo),因此GetType()调用将始终返回System.Type的类型.你应该改为typeof(Foo).GetInterfaces() (2认同)

小智 9

作为辅助方法的扩展

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);
}
Run Code Online (Sandbox Code Playgroud)

用法示例:

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

  • 这不适用于提问者不知道泛型类型参数的要求.从您的示例testObject.GetType().Implements(typeof(IDictionary <,>)); 将返回false. (17认同)
  • "IsAssignableFrom"正是我所寻找的 - 谢谢 (2认同)

Ben*_*ter 5

我使用的是@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));
}
Run Code Online (Sandbox Code Playgroud)

用法:

    if (!featureType.Implements<IFeature>())
        throw new InvalidCastException();
Run Code Online (Sandbox Code Playgroud)

  • 按照原始问题对通用接口的要求,它仍然不起作用。 (3认同)

Seb*_*ood 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));

    }
Run Code Online (Sandbox Code Playgroud)


Phi*_*tle 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);
    }
Run Code Online (Sandbox Code Playgroud)