Search code examples
c#.netcastle-windsorioc-containerfluent-interface

How can I simplify the registration of a large set of closed generic versions of the same open generic implementation?


Is there a fluent way of writing the following:

var someTypes = GetType()
    .Assembly
    .GetTypes()
    .Where(x => someFilter == true);

foreach(var someType in someTypes)
{
    var genericInterface = typeof(IFoo<>).MakeGenericType(someType);

    var genericImplementation = typeof(Foo<>).MakeGenericType(someType);

    container.Register(
       Component.For(genericInterface)
            .ImplementedBy(genericImplementation));
}

Solution

  • The following should to it

    container.Register(
        Component.For(typeof(IFoo<>))
          .ImplementedBy(typeof(Foo<>))
    );