如何确定类型是否实现了带有C#反射的接口
如何确定类型是否实现了带有C#反射的接口,c#,reflection,interface,C#,Reflection,Interface,C中的反射是否提供了一种方法来确定某些给定的System.TypeType是否为某些接口建模 使用: 或 您有几个选择: typeof(IMyInterface)。IsAssignableFrom(typeof(MyType)) typeof(MyType).GetInterfaces()包含(typeof(IMyInterface)) 对于通用接口,它有点不同 typeof(MyType).GetInterfaces().Any(i => i.IsGenericType &&am
C
中的反射是否提供了一种方法来确定某些给定的System.Type
Type是否为某些接口建模
使用:
或
您有几个选择:
typeof(IMyInterface)。IsAssignableFrom(typeof(MyType))
typeof(MyType).GetInterfaces()包含(typeof(IMyInterface))
typeof(MyType).GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMyInterface<>))
typeof(MyType).GetInterfaces().Any(i=>i.IsGenericType&&i.GetGenericTypeDefinition()==typeof(IMyInterface))
公共静态bool实现接口(此类型,类型ifaceType)
{
Type[]intf=Type.GetInterfaces();
for(inti=0;i
我认为这是正确的版本,原因有三:
正如其他人已经提到的: 本杰明2013年4月10日22:21”
当然很容易不去注意,也很容易不去争论 我可以从后面签名。我现在将使用GetInterfaces:p- 好的,另一种方法是创建一个简短的扩展方法,在某种程度上满足“最常见”的思维方式(并同意这是一个非常小的个人选择,根据个人喜好使其稍微“更自然”): 为什么不更一般一点(我不确定它是否真的那么有趣,我想我只是在传递另一撮‘合成’糖):
公共静态类类型扩展
{
公共静态bool IsAssignableTo(此类型,类型assignableType)
{
返回assignableType.IsAssignableFrom(类型);
}
公共静态bool IsAssignableTo(此类型)
{
返回IsAssignableTo(type,typeof(TAssignable));
}
}
我认为这样做可能更自然,但再一次,这只是一个非常个人观点的问题:
var isTrue = michelleType.IsAssignableTo<IMaBelle>();
var isTrue=michelleType.IsAssignableTo();
修改Jeff的答案以获得最佳性能(感谢Pierre Arnaud的性能测试):
要查找在给定的程序集中实现接口的所有类型
:
var implementations = typeof(TypeInTargetAssembly).Assembly.GetTypes()
.Where(t => typeof(IMyInterface).IsAssignableFrom(t) && t.IsClass);
那怎么办
if(MyType as IMyInterface != null)
typeof(IWhatever).GetTypeInfo().IsInterface
?怎么样
if(MyType as IMyInterface != null)
typeof(IWhatever).GetTypeInfo().IsInterface
我只是:
public static bool Implements<I>(this Type source) where I : class
{
return typeof(I).IsAssignableFrom(source);
}
我刚才说
实现
,因为这更直观。我总是将IsAssignableFrom
触发器设置为触发器。IsAssignableFrom
现在移动到类型信息
:
typeof(ISMSRequest).GetTypeInfo().IsAssignableFrom(typeof(T).GetTypeInfo());
正确答案是
typeof(MyType).GetInterface(nameof(IMyInterface)) != null;
但是,
typeof(MyType).IsAssignableFrom(typeof(IMyInterface));
可能返回错误的结果,如以下代码所示,其中包含string和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
请注意,如果您有一个通用接口
IMyInterface
,那么它将始终返回false
:
typeof(IMyInterface<>).IsAssignableFrom(typeof(MyType)) /* ALWAYS FALSE */
typeof(IMyInterface<MyType>).IsAssignableFrom(typeof(MyType))
但是,您可能在运行时不知道类型参数T
typeof(MyType).GetInterfaces()
.Any(x=>x.Name == typeof(IMyInterface<>).Name)
(请注意,上面使用的linq可能比循环慢。)
然后,您可以执行以下操作:
typeof(MyType).IsImplementing(IMyInterface<>)
typeof(MyType).正在实现(IMyInterface)
如果您有一个类型或实例,您可以轻松地检查它们是否支持特定的接口
要测试对象是否实现某个接口,请执行以下操作:
if(myObject is IMyInterface) {
// object myObject implements IMyInterface
}
if(typeof(IMyInterface).IsAssignableFrom(typeof(MyType))) {
// type MyType implements 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);
}
}
公共类类型扩展测试
{
[理论]
[InlineData(typeof(string)、typeof(IList)、false)]
[InlineData(typeof(List)、typeof(IList)、false)]
[InlineData(typeof(List)、typeof(IList)、true)]
[InlineData(typeof(List)、typeof(IList)、true)]
[InlineData(typeof(List)、typeof(IList)、true)]
[InlineData(typeof(List)、typeof(IList)、false)]
public void validateypeeimplementsinterface(类型Type,类型@interface,bool expect)
{
var output=type.ImplementsInterface(@interface);
等于(期望、输出);
}
}
如果您不需要使用反射,并且您有一个对象,您可以使用:
if(myObject is IMyInterface )
{
// it's implementing IMyInterface
}
使用(从.NET 5.0开始):
正如在几条评论中所述,IsAssignableFrom可能被认为是“向后”的混淆“如果你已经有了一个类的实例,一个更好的方法就是
someclass是IMyInterface
,因为这根本不涉及反射成本。所以,虽然没有错,但这不是一个理想的方法。@James-Agree。即使是Resharper也给出了同样的建议。@JamesJ.ReganIV你应该把它作为一个答案,我几乎错过了你的答案comment@reggaeguitar,谢谢,但是评论没有回答原来的问题。这个问题要求使用反射解决方案,我只是说,在这个答案的第一个例子中,对象反射的实例并不是理想的解决方案。@JamesJ.ReganIV实际上,是在继承层次结构的两个方向上检查,而IsAssignableFrom
只向上检查。另外,如果您有一个对象的实例,您应该调用IsInstanceOfType
(它也只能向上看)。记住typeof(IMyInterface)。IsAssignableFrom(typeof(IMyInterface))也是true,这可能会对您的代码产生意外的结果。很容易不引起注意并从
向后获取IsAssignableFrom的参数。现在我将使用GetInterfaces
pIsAssignableFrom(t1)
variant
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);
}
}
typeof(MyType).IsImplementing(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;
}
}
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);
}
}
if(myObject is IMyInterface )
{
// it's implementing IMyInterface
}
typeof(MyType).IsAssignableTo(typeof(IMyInterface));