Let's say I have got an interface and classes: (for asking purpose it will empty)
public interface IPerson { }
public class Tom : IPerson {}
public class Philip : IPerson {}
And a service interface and its implementation:
public interface IHelloService<T> where T : IPerson { }
public class HelloService<T> : IHelloService<T> where T : IPerson { }
How can I dynamically register service HelloService
for every IProdukt
I have got, I mean I want to register this classes:
IHelloService<Tom> -> HelloService<Tom>
IHelloService<Philip> -> HelloService<Philip>
Maybe it's something like this you're after:
container.Register(Component.For(typeof(IHelloService<>))
.ImplementedBy(typeof(HelloService<>), new Only<IProduct>())
.LifestyleTransient());
public class Only<T> : IGenericServiceStrategy
{
public bool Supports(Type service, ComponentModel component) => typeof(T).IsAssignableFrom(service.GetGenericArguments()[0]);
}
It will close the implementation with the same type argument as the service, but it will restrict possible type arguments to types that implement IProdukt/IPerson (I assume those are meant to be the same?).
See a description of this feature here: http://kozmic.net/2013/07/23/on-castle-windsor-and-open-generic-components/