Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Error with class inheriting from generic interface twice #870

Closed
starkcolin opened this issue Sep 26, 2017 · 2 comments
Closed

Error with class inheriting from generic interface twice #870

starkcolin opened this issue Sep 26, 2017 · 2 comments

Comments

@starkcolin
Copy link

starkcolin commented Sep 26, 2017

Version 4.6.1

When a class inherits from the same generic interface twice, an error message like below is shown:
The implementation type CGeneric'1 does not support the interface ISingleGenericWithInModifier'1.

The following test should pass:

public class TestClass
{
    public interface ISimpleInterface
    {
    }

    public interface ISingleGeneric<T>
    {
    }

    public interface ISingleGenericWithInModifier<in T>
    {
    }

    public interface ISingleGenericWithOutModifier<out T>
    {
    }

    public class CGeneric<T> : ISingleGeneric<T>
    {
    }

    public class CSimple : ISimpleInterface
    {
    }

    public class CDoubleInheritGeneric<T> : ISingleGenericWithInModifier<ISingleGeneric<T>>, ISingleGenericWithInModifier<CGeneric<T>>
    {
    }

    [Fact]
    public void CanResolveComponentWhenItInheritsFromTheSameGenericInterfaceTwice()
    {
        var builder = new ContainerBuilder();

        builder.RegisterGeneric(typeof(CDoubleInheritGeneric<>)).As(typeof(ISingleGenericWithInModifier<>));

        var container = builder.Build();

        // These should resolve because it inherits from ISingleGenericWithInModifier with both as nested generic arguments
        Assert.True(container.IsRegistered<ISingleGenericWithInModifier<ISingleGeneric<int>>>());
        Assert.True(container.IsRegistered<ISingleGenericWithInModifier<CGeneric<int>>>());
        Assert.True(container.IsRegistered<ISingleGenericWithInModifier<ISingleGeneric<CSimple>>>());
        Assert.True(container.IsRegistered<ISingleGenericWithInModifier<CGeneric<CSimple>>>());

        // These should not resolve because they do not match the above pattern
        Assert.False(container.IsRegistered<ISingleGenericWithInModifier<int>>());
        Assert.False(container.IsRegistered<ISingleGenericWithInModifier<ISingleGenericWithOutModifier<int>>>());
        Assert.False(container.IsRegistered<ISingleGenericWithInModifier<ISingleGenericWithOutModifier<CSimple>>>());
    }
}

The fix is to change the call from Single to First on line 109 of OpenGenericServiceBinder in method GetInterface.
I have tested it locally and all tests pass.

I have created PR #871 with the fix and test for the issue

@alexmg
Copy link
Member

alexmg commented Oct 2, 2017

Fixed by commit e58299b.

@alexmg alexmg closed this as completed Oct 2, 2017
@tillig
Copy link
Member

tillig commented Oct 16, 2017

Released as 4.6.2.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants