• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

111
Views
Comparing two System.Type for equality fails?

I've made this extension method to check if a type implements an interface. For it to work correctly it needs to compare 2 types. This comparison however doesn't seem to work realiably:

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;
    }
}

This is the case where my comparison fails:

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<>)));
    }
}

As mentioned in the comment, if I compare the type names then it always works as expected. I would like to know what's going on here.

about 3 years ago · Santiago Trujillo
1 answers
Answer question

0

If the interface is generic you need to be comparing back to the generic type definition:

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;
    }
}

This works for a bunch of test cases:

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

Live example: https://dotnetfiddle.net/bBslxH

about 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error