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(MyType as IMyInterface != null)
?
其他回答
你有几个选择:
typeof (IMyInterface) .IsAssignableFrom (typeof (MyType)) typeof (MyType) .GetInterfaces () .Contains (typeof (IMyInterface)) 在c# 6中,你可以使用typeof(MyType).GetInterface(nameof(IMyInterface)) != null
对于通用接口来说,这有点不同。
typeof(MyType).GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMyInterface<>))
如果您有一个类型或实例,您可以很容易地检查它们是否支持特定的接口。
测试一个对象是否实现了某个接口:
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));
可能会返回错误的结果,如下面的代码所示:
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
typeof(IMyInterface).IsAssignableFrom(someclass.GetType());
or
typeof(IMyInterface).IsAssignableFrom(typeof(MyType));