Yip*_*Yay 527 c# reflection interface
是否反映在C#
报价的方式来确定是否给予一些System.Type
款型的一些接口?
public interface IMyInterface {}
public class MyType : IMyInterface {}
// should yield 'true'
typeof(MyType)./* ????? */MODELS_INTERFACE(IMyInterface);
Run Code Online (Sandbox Code Playgroud)
Jef*_*eff 894
我有一些选择
typeof(IMyInterface).IsAssignableFrom(typeof(MyType))
typeof(MyType).GetInterfaces().Contains(typeof(IMyInterface))
对于通用接口,它有点不同.
typeof(MyType).GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMyInterface<>))
Run Code Online (Sandbox Code Playgroud)
Sne*_*nea 64
typeof(IMyInterface).IsAssignableFrom(typeof(MyType));
Run Code Online (Sandbox Code Playgroud)
ajm*_*jma 29
typeof(IMyInterface).IsAssignableFrom(someclass.GetType());
Run Code Online (Sandbox Code Playgroud)
要么
typeof(IMyInterface).IsAssignableFrom(typeof(MyType));
Run Code Online (Sandbox Code Playgroud)
Pan*_*eof 12
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;
}
Run Code Online (Sandbox Code Playgroud)
我认为这是正确的版本,原因有三:
1)它使用GetInterfaces而不是IsAssignableFrom,因为IsAssignableFrom最终在几次检查之后调用GetInterfaces,所以它更快.
2)它遍历本地数组,因此不会进行边界检查.
3)它使用为Type定义的==运算符,因此可能比Equals方法(Contains调用最终将使用)更安全.
我已经做了:
public static bool Implements<I>(this Type source) where I : class
{
return typeof(I).IsAssignableFrom(source);
}
Run Code Online (Sandbox Code Playgroud)
我希望我能说where I : interface
,但interface
不是通用的参数约束选项.class
尽可能接近.
用法:
if(MyType.Implements<IInitializable>())
MyCollection.Initialize();
Run Code Online (Sandbox Code Playgroud)
我刚才说,Implements
因为这更直观.我总是IsAssignableFrom
翻转.
正如其他人已经提到的那样:Benjamin 2013年4月10日22:21"
肯定很容易不注意并且反过来获得IsAssignableFrom的论据.我现在将使用GetInterfaces:p -
那么,另一种方法就是创建一种简短的扩展方法,在某种程度上实现"最常见"的思维方式(同意这是一个非常小的个人选择,根据一个人的偏好使其略微"更自然" ):
public static class TypeExtensions
{
public static bool IsAssignableTo(this Type type, Type assignableType)
{
return assignableType.IsAssignableFrom(type);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么不去更通用(不确定它是否真的那么有趣,我想我只是传递了另一个'语法'糖):
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));
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这可能更自然,但再一次只是个人意见的问题:
var isTrue = michelleType.IsAssignableTo<IMaBelle>();
Run Code Online (Sandbox Code Playgroud)
修改Jeff的答案以获得最佳性能(感谢Pierre Arnaud的性能测试):
var type = typeof(MyType);
var implementsInterface = typeof(IMyInterface).IsAssignableFrom(type) && type.IsClass;
Run Code Online (Sandbox Code Playgroud)
要查找在给定中实现接口的所有类型Assembly
:
var implementations = typeof(TypeInTargetAssembly).Assembly.GetTypes()
.Where(t => typeof(IMyInterface).IsAssignableFrom(t) && t.IsClass);
Run Code Online (Sandbox Code Playgroud)
请注意,如果您有通用接口IMyInterface<T>
,那么这将始终返回false
:
typeof(IMyInterface<>).IsAssignableFrom(typeof(MyType)) /* ALWAYS FALSE */
Run Code Online (Sandbox Code Playgroud)
这也不起作用:
typeof(MyType).GetInterfaces().Contains(typeof(IMyInterface<>)) /* ALWAYS FALSE */
Run Code Online (Sandbox Code Playgroud)
但是,如果MyType
实现IMyInterface<MyType>
此功能并返回true
:
typeof(IMyInterface<MyType>).IsAssignableFrom(typeof(MyType))
Run Code Online (Sandbox Code Playgroud)
T
但是,您可能在运行时不知道参数的类型。一个有点hacky的解决方案是:
typeof(MyType).GetInterfaces()
.Any(x=>x.Name == typeof(IMyInterface<>).Name)
Run Code Online (Sandbox Code Playgroud)
杰夫的解决方案不那么老套:
typeof(MyType).GetInterfaces()
.Any(i => i.IsGenericType
&& i.GetGenericTypeDefinition() == typeof(IMyInterface<>));
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
(请注意,上面使用了 linq,它可能比循环慢。)
然后你可以这样做:
typeof(MyType).IsImplementing(IMyInterface<>)
Run Code Online (Sandbox Code Playgroud)
小智 6
如果您有类型或实例,您可以轻松检查它们是否支持特定接口。
测试一个对象是否实现了某个接口:
if(myObject is IMyInterface) {
// object myObject implements IMyInterface
}
Run Code Online (Sandbox Code Playgroud)
测试一个类型是否实现了某个接口:
if(typeof(IMyInterface).IsAssignableFrom(typeof(MyType))) {
// type MyType implements IMyInterface
}
Run Code Online (Sandbox Code Playgroud)
如果您有一个通用对象并想要进行转换以及检查您转换到的接口是否已实现,则代码为:
var myCastedObject = myObject as IMyInterface;
if(myCastedObject != null) {
// object myObject implements IMyInterface
}
Run Code Online (Sandbox Code Playgroud)
搜索此内容的任何人可能会发现以下扩展方法很有用:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
使用Type.IsAssignableTo
(从 .NET 5.0 开始):
typeof(MyType).IsAssignableTo(typeof(IMyInterface));
Run Code Online (Sandbox Code Playgroud)
正如几条评论中所述,IsAssignableFrom 可能被认为是“落后”的混淆。