Grace icon indicating copy to clipboard operation
Grace copied to clipboard

Use target type as generic parameter

Open mgth opened this issue 5 years ago • 3 comments

Hello,

I need something like :

Export<Generic<>>().As<ISomethingNotGeneric>().Where(generic parameter is "InjectedInto" object type)

Is there any existing way to do this ? Thank you.

mgth avatar Nov 23 '18 18:11 mgth

Hi @mgth

There are methods for selecting when to export an implementation using the When property.

I've attached a test to show how you could create a C# extension to do what you want here.

ipjohnson avatar Nov 24 '18 19:11 ipjohnson

Thank you for the response (And for that great piece of code), but I think my explanation was not so clear. Lets say :

class TestClass { 
    [Import]
    public IGenericService Service;
}

class GenericService<T> : IGenericService
{
}

And I would like that :

            var instance = container.Locate<TestClass>();

            Assert.NotNull(instance);
            Assert.IsType<GenericService<TestClass>>(instance.Service);

mgth avatar Nov 26 '18 08:11 mgth

Hi @mgth

Sorry for the late response. It's not really possible without writing some code. If IGenericService had a T it could be resolved.

I think this is an interesting feature. I'll start looking at adding support.

ipjohnson avatar Dec 02 '18 12:12 ipjohnson