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

Attempting to resolve a non-generic type with a backing generic implementation causes an exception to be thrown #30

Closed
mvastarelli opened this issue Aug 11, 2013 · 3 comments

Comments

@mvastarelli
Copy link
Contributor

If I have a non-generic interface that's implemented using a parameterized type, when I attempt to resolve it an exception will be thrown stating that the container doesn't have enough information to determine what to construct (expected). However, if I add a IGenericImplementationMatchingStrategy that provides the parameter type, then an Invalid Operation exception will be thrown when calling Type.GetGenericTypeDefinition() from within DefaultGenericHandler.AdaptServices().

@kkozmic
Copy link
Contributor

kkozmic commented Aug 11, 2013

hey,
can you build a test demonstrating the scenario?

@mvastarelli
Copy link
Contributor Author

This is a bit contrived, but the following code/test will fail.

public interface IServiceProvider
{
    string DoWork(string data);
}

public class ServiceProviderImp<T> : IServiceProvider
{
    public string DoWork(string data)
    {
        return null;
    }
}

public class ServiceProviderGenericStrategy : IGenericImplementationMatchingStrategy
{
    public Type[] GetGenericArguments(ComponentModel model, CreationContext context)
    {
        return new[] { typeof(string) };
    }
}

public class ServiceProviderTest
{
    [Fact]
    public void AttemptToResolveServiceProvider()
    {
        // Arrange.
        var container = new WindsorContainer();

        // Act.
        container.Register(Component.For<IServiceProvider>().ImplementedBy(typeof(ServiceProviderImp<>), new ServiceProviderGenericStrategy()));

        // Assert.
        Assert.DoesNotThrow(() => container.Resolve<IServiceProvider>());
    }
}

@kkozmic
Copy link
Contributor

kkozmic commented Sep 3, 2013

Hey,

Finally had some time to look at this. It seems you're going slightly off the beaten path here and register it like that, instead of just going:

Component.For<IServiceProvider>().ImplementedBy<ServiceProviderImp<string>>()

Would that suffice in your case, or is there something more interesting happening in your actual registration?

kkozmic added a commit that referenced this issue Sep 3, 2013
@kkozmic kkozmic closed this as completed Sep 3, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants