天天看点

如何确定类型是否使用C#反射实现接口

C#

反射是否提供确定某些给定的

System.Type

类型是否为某些接口建模的方法?

public interface IMyInterface {}

public class MyType : IMyInterface {}

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

#1楼

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;
}
           

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

  1. 它使用GetInterfaces而不是IsAssignableFrom,它的速度更快,因为IsAssignableFrom最终在多次检查后都会调用GetInterfaces。
  2. 它遍历本地数组,因此不会进行边界检查。
  3. 它使用为Type定义的==运算符,因此可能比Equals方法(包含调用最终将使用)更安全。

#2楼

正如其他人已经提到的:本杰明·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>();
           

#3楼

修改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);
           

#4楼

关于什么

if(MyType as IMyInterface != null)
           

#5楼

关于什么

typeof(IWhatever).GetTypeInfo().IsInterface
           

#6楼

我已经做了:

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

我希望我可以在

where I : interface

where I : interface

,但是

interface

不是通用参数约束选项。

class

就近了。

用法:

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

我刚刚说了

Implements

因为它更直观。 我总是得到

IsAssignableFrom

触发器。

#7楼

IsAssignableFrom

现在移到

TypeInfo

typeof(ISMSRequest).GetTypeInfo().IsAssignableFrom(typeof(T).GetTypeInfo());
           

#8楼

正确的答案是

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

#9楼

使用

Type.IsAssignableFrom

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

#10楼

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

要么

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

#11楼

您有几种选择:

  1. typeof(IMyInterface).IsAssignableFrom(typeof(MyType))

  2. typeof(MyType).GetInterfaces().Contains(typeof(IMyInterface))

对于通用接口,则有所不同。

typeof(MyType).GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMyInterface<>))
           

#12楼

请注意,如果您具有通用接口

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

一个有点古怪的解决方案是:

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<>)
           

#13楼

如果您有类型或实例,则可以轻松检查它们是否支持特定的接口。

要测试对象是否实现特定接口:

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
    }