C# 测试对象是否实现接口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/410227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Test if object implements interface
提问by JoshRivers
采纳答案by Robert C. Barth
if (object is IBlah)
or
或者
IBlah myTest = originalObject as IBlah
if (myTest != null)
回答by Rauhotz
For the instance:
例如:
if (obj is IMyInterface) {}
For the class:
对于班级:
Check if typeof(MyClass).GetInterfaces()
contains the interface.
检查是否typeof(MyClass).GetInterfaces()
包含接口。
回答by Yoann. B
This should work :
这应该工作:
MyInstace.GetType().GetInterfaces();
But nice too :
但也不错:
if (obj is IMyInterface)
Or even (not very elegant) :
甚至(不是很优雅):
if (obj.GetType() == typeof(IMyInterface))
回答by jamesmillerio
In addition to testing using the "is" operator, you can decorate your methods to make sure that variables passed to it implement a particular interface, like so:
除了使用“is”运算符进行测试之外,您还可以修饰您的方法以确保传递给它的变量实现特定的接口,如下所示:
public static void BubbleSort<T>(ref IList<T> unsorted_list) where T : IComparable
{
//Some bubbly sorting
}
I'm not sure which version of .Net this was implemented in so it may not work in your version.
我不确定这是在哪个版本的 .Net 中实现的,所以它可能不适用于您的版本。
回答by Andrew Kennan
Using the is
or as
operators is the correct way if you know the interface type at compile time and have an instance of the type you are testing. Something that no one else seems to have mentioned is Type.IsAssignableFrom
:
如果您在编译时知道接口类型并且有您正在测试的类型的实例,则使用is
oras
运算符是正确的方法。其他人似乎没有提到的事情是Type.IsAssignableFrom
:
if( typeof(IMyInterface).IsAssignableFrom(someOtherType) )
{
}
I think this is much neater than looking through the array returned by GetInterfaces
and has the advantage of working for classes as well.
我认为这比查看返回的数组要简洁得多,GetInterfaces
并且还具有为类工作的优势。
回答by famousgarkin
A variation on @AndrewKennan's answer I ended up using recently for types obtained at runtime:
@AndrewKennan's answer的一个变体我最近最终用于在运行时获得的类型:
if (serviceType.IsInstanceOfType(service))
{
// 'service' does implement the 'serviceType' type
}
回答by iamrcw
I used
我用了
Assert.IsTrue(myObject is ImyInterface);
Assert.IsTrue(myObject is ImyInterface);
for a test in my unit test which tests that myObject is an object which has implemented my interface ImyInterface.
对于我的单元测试中的一个测试,它测试 myObject 是一个实现了我的接口 ImyInterface 的对象。
回答by jahu
Recently I tried using Andrew Kennan's answer and it didn't work for me for some reason. I used this instead and it worked (note: writing the namespace might be required).
最近我尝试使用 Andrew Kennan 的答案,但由于某种原因它对我不起作用。我改用它并且它起作用了(注意:可能需要编写命名空间)。
if (typeof(someObject).GetInterface("MyNamespace.IMyInterface") != null)
回答by Dutchman078
What worked for me is:
对我有用的是:
Assert.IsNotNull(typeof (YourClass).GetInterfaces().SingleOrDefault(i => i == typeof (ISomeInterface)));
Assert.IsNotNull(typeof (YourClass).GetInterfaces().SingleOrDefault(i => i == typeof (ISomeInterface)));