c#中的反射是否提供了一种方法来确定某个给定的System。类型类型模型一些接口?
public interface IMyInterface {}
public class MyType : IMyInterface {}
// should yield 'true'
typeof(MyType)./* ????? */MODELS_INTERFACE(IMyInterface);
c#中的反射是否提供了一种方法来确定某个给定的System。类型类型模型一些接口?
public interface IMyInterface {}
public class MyType : IMyInterface {}
// should yield 'true'
typeof(MyType)./* ????? */MODELS_INTERFACE(IMyInterface);
当前回答
如果你不需要使用反射,你有一个对象,你可以使用这个:
if(myObject is IMyInterface )
{
// it's implementing IMyInterface
}
其他回答
使用类型。IsAssignableFrom:
typeof(IMyInterface).IsAssignableFrom(typeof(MyType));
如果你不需要使用反射,你有一个对象,你可以使用这个:
if(myObject is IMyInterface )
{
// it's implementing 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(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
注意,如果你有一个通用接口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)
杰夫的解决方案就没那么俗气了:
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<>)