Castle Windsor - IoC registration for open generic interfaces?

13,641

It's called open generic, and yes, Windsor does support that.

 container.Register(Component
             .For(typeof(IAdapterFactory<,>))
             .ImplementedBy(typeof(AdapterFactory<,>))
             .LifestylePerWebRequest());
Share:
13,641
goldfinger
Author by

goldfinger

Updated on June 03, 2022

Comments

  • goldfinger
    goldfinger about 2 years

    Does Castle Windsor permit registration of an open generic interface or do I need to register each possible typed instance separately?

    Example - the below with types T,Z fails upon compilation unless I separately specify T, Z with strong types.

     container.Register(Component
          .For<IAdapterFactory<T,Z>>()
          .ImplementedBy<AdapterFactory<T,Z>>()
          .LifeStyle.PerWebRequest);
    
  • goldfinger
    goldfinger almost 12 years
    One word: wow. Thank you! Just out of curiosity, wonder why Castle folks decided to deviate from more standard C# syntax - <T,Z> instead of <,>?
  • Krzysztof Kozmic
    Krzysztof Kozmic almost 12 years
    This is the only syntax that will work in this scenario and it has noting to do with Windsor. This just is how you obtain an instance of System.Type representing an open generic type.
  • Richard Bailey
    Richard Bailey over 3 years
    Probable the fifth time this answer helped me out 🙄 - thank you