Grace
Grace copied to clipboard
Use target type as generic parameter
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.
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.
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);
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.