c#中的反射是否提供了一种方法来确定某个给定的System。类型类型模型一些接口?

public interface IMyInterface {}

public class MyType : IMyInterface {}

// should yield 'true'
typeof(MyType)./* ????? */MODELS_INTERFACE(IMyInterface);

当前回答

我刚刚做了:

public static bool Implements<I>(this Type source) where I : class
{
  return typeof(I).IsAssignableFrom(source);
}

我希望我能说在哪里I: interface,但interface不是一个通用参数约束选项。课堂离我们很近。

用法:

if(MyType.Implements<IInitializable>())
  MyCollection.Initialize();

我只是说工具,因为这更直观。我总是得到IsAssignableFrom flip-flopped。

其他回答

typeof(IMyInterface).IsAssignableFrom(someclass.GetType());

or

typeof(IMyInterface).IsAssignableFrom(typeof(MyType));
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;
}

我认为这是一个正确的版本,原因有三:

它使用GetInterfaces而不是IsAssignableFrom,因为它更快 IsAssignableFrom最终在几次检查后调用 getinterface。 它遍历本地数组,所以会有 没有边界检查。 它使用为定义的==操作符 类型,因此可能比Equals方法更安全 调用,将最终使用)。

正确答案是

typeof(MyType).GetInterface(nameof(IMyInterface)) != null;

然而,

typeof(MyType).IsAssignableFrom(typeof(IMyInterface));

可能会返回错误的结果,如下面的代码所示:

    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: True

我刚刚做了:

public static bool Implements<I>(this Type source) where I : class
{
  return typeof(I).IsAssignableFrom(source);
}

我希望我能说在哪里I: interface,但interface不是一个通用参数约束选项。课堂离我们很近。

用法:

if(MyType.Implements<IInitializable>())
  MyCollection.Initialize();

我只是说工具,因为这更直观。我总是得到IsAssignableFrom flip-flopped。

使用类型。IsAssignableTo(自。net 5.0起):

typeof(MyType).IsAssignableTo(typeof(IMyInterface));

正如在一些评论中所述,IsAssignableFrom可能被认为是“向后”的混淆。