ipjohnson / Grace

Grace is a feature rich dependency injection container library
MIT License
336 stars 33 forks source link

Use target type as generic parameter #199

Open mgth opened 5 years ago

mgth commented 5 years ago

Hello,

I need something like :

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

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

ipjohnson commented 5 years ago

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.

mgth commented 5 years ago

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);
ipjohnson commented 5 years ago

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.