Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Generic parameters constrained with complex generic types fail to resolve. #421

Closed
alexmg opened this issue Jan 22, 2014 · 4 comments
Closed
Labels

Comments

@alexmg
Copy link
Member

alexmg commented Jan 22, 2014

From [email protected] on April 03, 2013 22:44:08

What steps will reproduce the problem? Here is the situation with 2 failing tests :

    public interface IConstraint<T> { }

    public class Constrained<T1, T2>
        where T2 : IConstraint<T1>
    {
    }
    public interface IConstraintWithAddedArgument<T2, T1> : IConstraint<T1> { }

    [Test]
    public void CanResolveComponentWhenConstrainedArgumentIsGenericTypeWithMoreArgumentsThanGenericConstraint()
    {
        var builder = new ContainerBuilder();

        builder.RegisterGeneric(typeof(Constrained<,>));

        var container = builder.Build();

        Assert.That(container.IsRegistered<Constrained<int, IConstraintWithAddedArgument<string, int>>>());
    }


    public interface IConstrainedConstraint<T>
        where T : IEquatable<int> { }

    public interface IConstrainedConstraintWithAddedArgument<T1, T2> : IConstrainedConstraint<T2>
        where T2 : IEquatable<int>
    { }

    public interface IConstrainedConstraintWithOnlyAddedArgument<T1> : IConstrainedConstraintWithAddedArgument<T1, int>
    { }

    public class MultiConstrained<T1, T2>
      where T1 : IEquatable<int>
      where T2 : IConstrainedConstraint<T1>
    {
    }

    [Test]
    public void CanResolveComponentWhenConstraintsAreNested()
    {
        var builder = new ContainerBuilder();

        builder.RegisterGeneric(typeof(MultiConstrained<,>));

        var container = builder.Build();

        Assert.That(container.IsRegistered<MultiConstrained<int, IConstrainedConstraintWithOnlyAddedArgument<string>>>());
    } What is the expected output? What do you see instead? The requested services should be registred, but the following exceptions is thrown :

Autofac.Tests.Features. OpenGenerics .ComplexGenericsTests.CanResolveComponentWhenConstrainedArgumentIsGenericTypeWithMoreArgumentsThanGenericConstraint:
System.ArgumentException : The number of generic arguments provided doesn't equal the arity of the generic type definition.
Parameter name: instantiation

or

Autofac.Tests.Features. OpenGenerics .ComplexGenericsTests.CanResolveComponentWhenConstraintsAreNested:
System.ArgumentException : GenericArguments[0], 'System.String', on 'Autofac.Tests.Features. OpenGenerics .ComplexGenericsTests+IConstrainedConstraint1[T]' violates the constraint of type 'T'. ----> System.TypeLoadException : GenericArguments[0], 'System.String', on 'Autofac.Tests.Features. OpenGenerics .ComplexGenericsTests+IConstrainedConstraint1[T]' violates the constraint of type parameter 'T'.

The problem is in ParameterEqualsConstraint in TypeExtensions.cs. It's missing test cases.
Checking number of arguments and catching all others, all works correctly.

Attachment: ComplexGenericsTests.cs.patch TypeExtensions.cs.patch

Original issue: http://code.google.com/p/autofac/issues/detail?id=421

@alexmg
Copy link
Member Author

alexmg commented Jan 22, 2014

From travis.illig on April 04, 2013 07:58:09

Thanks for reporting this and extra-super-thanks for including both a failing test and a patch! We'll take a look as soon as we can.

Labels: Module-Core

@alexmg
Copy link
Member Author

alexmg commented Jan 22, 2014

From alex.meyergleaves on April 05, 2013 20:09:35

Owner: alex.meyergleaves

@alexmg
Copy link
Member Author

alexmg commented Jan 22, 2014

From alex.meyergleaves on April 05, 2013 20:19:56

This issue was closed by revision 107b5d93cafe .

Status: Fixed

@alexmg
Copy link
Member Author

alexmg commented Jan 22, 2014

From travis.illig on April 09, 2013 16:04:06

Issue 422 has been merged into this issue.

@alexmg alexmg closed this as completed Jan 22, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant