Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.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/2/.net/21.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#_.net - Fatal编程技术网

C# 比较两个系统。相等类型失败?

C# 比较两个系统。相等类型失败?,c#,.net,C#,.net,我使用这个扩展方法来检查类型是否实现了接口。要使其正常工作,需要比较两种类型。然而,这种比较似乎并不实际有效: public static bool ImplementsInterface(this Type type, Type testInterface) { if (testInterface.GenericTypeArguments.Length > 0) { return testInterface.IsAssignableFrom(type);

我使用这个扩展方法来检查类型是否实现了接口。要使其正常工作,需要比较两种类型。然而,这种比较似乎并不实际有效:

public static bool ImplementsInterface(this Type type, Type testInterface)
{
    if (testInterface.GenericTypeArguments.Length > 0)
    {
        return testInterface.IsAssignableFrom(type);
    }
    else
    {
        foreach (var @interface in type.GetInterfaces())
        {
            // This doesn't always work:
            if (@interface == testInterface)
            // But comparing the names instead always works!
            // if (@interface.Name == testInterface.Name)
            {
                return true;
            }
        }
        return false;
    }
}
这种情况下,我的比较失败:

public static class TestInterfaceExtensions
{
    interface I1 { }
    interface I2<T> : I1 { }
    class Class1Int : I2<int> { }

    [Fact]
    public void ImplementsInterface()
    {
        Assert.True(typeof(Class1Int).ImplementsInterface(typeof(I2<>)));
    }
}
公共静态类TestInterfaceExtensions
{
接口I1{}
接口I2:I1{}
类Class1Int:I2{}
[事实]
public void ImplementsInterface()
{
True(typeof(classaint).ImplementsInterface(typeof(I2));
}
}

正如注释中提到的,如果我比较类型名称,那么它总是按照预期工作。我想知道这里发生了什么。

如果接口是泛型的,则需要与泛型类型定义进行比较:

public static bool ImplementsInterface(this Type type, Type testInterface)
{
    if (testInterface.GenericTypeArguments.Length > 0)
    {
        return testInterface.IsAssignableFrom(type);
    }
    else
    {
        foreach (var @interface in type.GetInterfaces())
        {
            var compareType = @interface.IsGenericType
                ? @interface.GetGenericTypeDefinition()
                : @interface;
            if (compareType == testInterface)
            {
                return true;
            }
        }
        return false;
    }
}
这适用于一系列测试用例:

Console.WriteLine(typeof(Class1Int).ImplementsInterface(typeof(I2<>)));     // True
Console.WriteLine(typeof(Class1Int).ImplementsInterface(typeof(I2<int>)));  // True
Console.WriteLine(typeof(Class1Int).ImplementsInterface(typeof(I2<bool>))); // False
Console.WriteLine(typeof(Class1Int).ImplementsInterface(typeof(I1)));       // True
Console.WriteLine(typeof(Class1Int).ImplementsInterface(typeof(I3)));       // False
Console.WriteLine(typeof(classaint).ImplementsInterface(typeof(I2));//真的
Console.WriteLine(typeof(classaint).ImplementsInterface(typeof(I2));//真的
Console.WriteLine(typeof(classaint).ImplementsInterface(typeof(I2));//假的
Console.WriteLine(typeof(classaint).ImplementsInterface(typeof(I1));//真的
Console.WriteLine(typeof(classaint).ImplementsInterface(typeof(I3));//假的

活生生的例子:

我的意思是,你在比较开放接口和封闭接口,所以它们当然不一样。但是肯定有一种方法可以做你正在尝试的事情。。。只需查看调试程序,检查此答案是否适用于您的应用程序。我明白了,所以未绑定的I2表达式不应该等于I2。字符串名称比较只是巧合,因为两种情况下的名称都是I2`1。谢谢你的回复!