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

StackOverflow https://stackoverflow.com/questions/17795982

Вопрос

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));
}
Это было полезно?

Решение 2

The following should to it

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

Другие советы

a IGenericServiceStrategy is what you're after (on top of what @maxlego said, which is correct)

See this for details and example.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top