Answers:
您有几种选择:
typeof(IMyInterface).IsAssignableFrom(typeof(MyType))
typeof(MyType).GetInterfaces().Contains(typeof(IMyInterface))
对于通用接口,则有所不同。
typeof(MyType).GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMyInterface<>))IsAssignableFrom倒退。我GetInterfaces现在就去:p
                    IsAssignableFrom(t1)变体比GetInterfaces().Contains(t2)我的代码中的变体快约3倍。
                    typeof(MyType).GetInterface(nameof(IMyInterface)) != null更好的类型安全性和重构。
                    typeof(IMyInterface).IsAssignableFrom(typeof(MyType));typeof(IMyInterface).IsAssignableFrom(someclass.GetType());要么
typeof(IMyInterface).IsAssignableFrom(typeof(MyType));someclass is IMyInterface不涉及反射成本。因此,虽然没有错,但这并不是实现它的理想方法。
                    is在继承层次结构的两个方向都进行检查,而IsAssignableFrom仅向上进行检查。另外,如果您有一个对象的实例,则应调用IsInstanceOfType(它也只向上看)。
                    public static bool ImplementsInterface(this Type type, Type ifaceType) 
{
    Type[] intf = type.GetInterfaces();
    for(int i = 0; i < intf.Length; i++) 
    {
        if(intf[ i ] == ifaceType) 
        {
            return true;
        }
    }
    return false;
}我认为这是正确的版本,原因有以下三个:
我已经做了:
public static bool Implements<I>(this Type source) where I : class
{
  return typeof(I).IsAssignableFrom(source);
}我希望我可以说过where I : interface,但interface不是通用参数约束选项。class尽可能地接近
用法:
if(MyType.Implements<IInitializable>())
  MyCollection.Initialize();我刚才说的Implements是因为这样更直观。我总是被IsAssignableFrom人为打翻。
return typeof(I).IsInterface && typeof(I).IsAssignableFrom(source);对方法的任何“不正确”用法返回false,即;将其与类类型而不是接口类型一起使用,或者,如果类型参数不是接口,则抛出异常。尽管您可能会争辩说派生类是“实现”的父类……
                    修改Jeff的答案以获得最佳性能(感谢Pierre Arnaud的性能测试):
var type = typeof(MyType);
var implementsInterface = typeof(IMyInterface).IsAssignableFrom(type) && type.IsClass;查找在给定中实现接口的所有类型Assembly:
var implementations = typeof(TypeInTargetAssembly).Assembly.GetTypes()
                          .Where(t => typeof(IMyInterface).IsAssignableFrom(t) && t.IsClass);正如其他人已经提到的:本杰明·13年4月10日在22:21”
可以很容易地不注意并向后获取IsAssignableFrom的参数。我现在就使用GetInterfaces:p –
好吧,另一种解决方法是创建一个简短的扩展方法,在某种程度上满足“最通常”的思维方式(并同意这是一个很小的个人选择,可以根据自己的喜好使其稍微“自然”些) ):
public static class TypeExtensions
{
    public static bool IsAssignableTo(this Type type, Type assignableType)
    {
        return assignableType.IsAssignableFrom(type);
    }
}为什么不更通用一些(不知道是否真的那么有趣,我想我只是在传递另一撮“语法化”糖):
public static class TypeExtensions
{
    public static bool IsAssignableTo(this Type type, Type assignableType)
    {
        return assignableType.IsAssignableFrom(type);
    }
    public static bool IsAssignableTo<TAssignable>(this Type type)
    {
        return IsAssignableTo(type, typeof(TAssignable));
    }
}我认为这样可能更自然,但再次只是个人意见的问题:
var isTrue = michelleType.IsAssignableTo<IMaBelle>();Boolean=> bool(我不知道为什么我年轻时曾经有一些严格的“花哨”编码规则)。
                    如果您有类型或实例,则可以轻松检查它们是否支持特定的接口。
要测试对象是否实现某个接口:
if(myObject is IMyInterface) {
  // object myObject implements IMyInterface
}要测试一种类型是否实现了某个接口:
if(typeof(IMyInterface).IsAssignableFrom(typeof(MyType))) {
  // type MyType implements IMyInterface
}如果您有一个通用对象,并且想要进行转换以及检查您转换到的接口是否已实现,则代码为:
 var myCastedObject = myObject as IMyInterface;
    if(myCastedObject != null) {
      // object myObject implements IMyInterface
    }任何搜索此内容的人都可能会发现以下扩展方法很有用:
public static class TypeExtensions
{
    public static bool ImplementsInterface(this Type type, Type @interface)
    {
        if (type == null)
        {
            throw new ArgumentNullException(nameof(type));
        }
        if (@interface == null)
        {
            throw new ArgumentNullException(nameof(@interface));
        }
        var interfaces = type.GetInterfaces();
        if (@interface.IsGenericTypeDefinition)
        {
            foreach (var item in interfaces)
            {
                if (item.IsConstructedGenericType && item.GetGenericTypeDefinition() == @interface)
                {
                    return true;
                }
            }
        }
        else
        {
            foreach (var item in interfaces)
            {
                if (item == @interface)
                {
                    return true;
                }
            }
        }
        return false;
    }
}xunit测试:
public class TypeExtensionTests
{
    [Theory]
    [InlineData(typeof(string), typeof(IList<int>), false)]
    [InlineData(typeof(List<>), typeof(IList<int>), false)]
    [InlineData(typeof(List<>), typeof(IList<>), true)]
    [InlineData(typeof(List<int>), typeof(IList<>), true)]
    [InlineData(typeof(List<int>), typeof(IList<int>), true)]
    [InlineData(typeof(List<int>), typeof(IList<string>), false)]
    public void ValidateTypeImplementsInterface(Type type, Type @interface, bool expect)
    {
        var output = type.ImplementsInterface(@interface);
        Assert.Equal(expect, output);
    }
}正确答案是
typeof(MyType).GetInterface(nameof(IMyInterface)) != null;然而,
typeof(MyType).IsAssignableFrom(typeof(IMyInterface));可能会返回错误的结果,如以下代码所示,字符串和IConvertible:
    static void TestIConvertible()
    {
        string test = "test";
        Type stringType = typeof(string); // or test.GetType();
        bool isConvertibleDirect = test is IConvertible;
        bool isConvertibleTypeAssignable = stringType.IsAssignableFrom(typeof(IConvertible));
        bool isConvertibleHasInterface = stringType.GetInterface(nameof(IConvertible)) != null;
        Console.WriteLine($"isConvertibleDirect: {isConvertibleDirect}");
        Console.WriteLine($"isConvertibleTypeAssignable: {isConvertibleTypeAssignable}");
        Console.WriteLine($"isConvertibleHasInterface: {isConvertibleHasInterface}");
    }结果:
 isConvertibleDirect: True
 isConvertibleTypeAssignable: False
 isConvertibleHasInterface: TrueIsAssignableFrom。就像本杰明(Benjamin)和伊万(Ehouarn)警告一样。
                    请注意,如果您具有通用接口,IMyInterface<T>则它将始终返回false:
  typeof(IMyInterface<>).IsAssignableFrom(typeof(MyType)) /* ALWAYS FALSE */这也不起作用:
  typeof(MyType).GetInterfaces().Contains(typeof(IMyInterface<>))  /* ALWAYS FALSE */但是,如果MyType实现IMyInterface<MyType>此功能,则返回true:
  typeof(IMyInterface<MyType>).IsAssignableFrom(typeof(MyType))但是,您可能T在运行时不知道type参数。一个有点古怪的解决方案是:
  typeof(MyType).GetInterfaces()
                .Any(x=>x.Name == typeof(IMyInterface<>).Name)Jeff的解决方案不太灵活:
  typeof(MyType).GetInterfaces()
         .Any(i => i.IsGenericType 
             && i.GetGenericTypeDefinition() == typeof(IMyInterface<>));这是一种Type适用于任何情况的扩展方法:
public static class TypeExtensions
{
    public static bool IsImplementing(this Type type, Type someInterface)
    {
        return type.GetInterfaces()
             .Any(i => i == someInterface 
                 || i.IsGenericType 
                    && i.GetGenericTypeDefinition() == someInterface);
    }
}(请注意,上面使用的是linq,它可能比循环慢。)
然后,您可以执行以下操作:
   typeof(MyType).IsImplementing(IMyInterface<>)