Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何确定类是否实现了特定接口_C#_Oop - Fatal编程技术网

C# 如何确定类是否实现了特定接口

C# 如何确定类是否实现了特定接口,c#,oop,C#,Oop,假设我有一门像这样的课 interface ISampleInterface { void SampleMethod(); } class ImplementationClass : ISampleInterface { // Explicit interface member implementation: void ISampleInterface.SampleMethod() { // Method implementation. } static void Main()

假设我有一门像这样的课

interface ISampleInterface
{
  void SampleMethod();
}

class ImplementationClass : ISampleInterface
{
// Explicit interface member implementation: 
void ISampleInterface.SampleMethod()
{
    // Method implementation.
}

static void Main()
{
    // Declare an interface instance.
    ISampleInterface obj = new ImplementationClass();

    // Call the member.
    obj.SampleMethod();
}
}
在编写如下代码之前,如何从main方法确定
ImplementationClass
类实现了
ISampleInterface

SampleInterface obj = new ImplementationClass();
obj.SampleMethod();
有没有办法……请讨论一下。谢谢

    public static bool IsImplementationOf(this Type checkMe, Type forMe)
    {
        foreach (Type iface in checkMe.GetInterfaces())
        {
            if (iface == forMe)
                return true;
        }

        return false;
    }
使用以下命令调用它:

if (obj.GetType().IsImplementationOf(typeof(SampleInterface)))
    Console.WriteLine("obj implements SampleInterface");
这是一个很好的解决方案。您可以测试一个对象是接口还是其他类。您可以这样做:

if (obj is ISampleInterface)
{
     //Yes, obj is compatible with ISampleInterface
}
如果在运行时没有对象的实例,但有
类型
,则可以使用IsAssignableFrom:

Type type = typeof(ISampleInterface);
var isassignable = type.IsAssignableFrom(otherType);

您可以使用反射:

bool result = typeof(ISampleInterface).IsAssignableFrom(typeof(ImplementationClass));

当您硬编码实现类时,您知道它实现了什么接口,因此您只需查看源代码或文档即可了解类实现了哪些接口

如果您正在接收未知类型的对象,有几种方法可以检查接口的实现:

void Test1(object a) {
    var x = a as IMyInterface;
    if (x != null) {
        // x implements IMyInterface, you can call SampleMethod
    }
}

void Test2(object a) {
    if (a is IMyInterface) {
        // a implements IMyInterface
        ((IMyInterface)a).SampleMethod();
    }
}
一种模式(推荐的)是


如果您需要知道这一点,那么您可能在执行时有一些东西-您是否有对象,或者只是类型的名称,或者什么?呃,请查看class@JonSkeet也许我错了,但我以为OP是在问如何在设计时确定它。@BenRobinson:基本上还不清楚……我猜是这样的“在编写代码之前,
SampleInterface obj=new ImplementationClass()“意味着他没有对象实例,希望对类型应用测试。
SampleInterface i = myObject as SampleInterface;
if (i != null) {
    // MyObject implements SampleInterface
}