Код: Выделить всё
// The thing
public interface IThing
{
void DoSomething();
}
public class ThingA : IThing
{
public void DoSomething() => throw new NotImplementedException();
}
public class ThingB : IThing
{
public void DoSomething() => throw new NotImplementedException();
}
// The factory
public interface IThingFactory where TThing : IThing
{
TThing CreateThing();
}
public class MyThingFactory : IThingFactory where TThing : IThing
{
public TThing CreateThing() => throw new NotImplementedException();
}
Код: Выделить всё
Services.AddSingleton();
...
var factory = ServiceProvider.GetRequiredService();
// factory is a MyThingFactory
Код: Выделить всё
Services.AddSingleton();
Тип MyThingFactory не может использоваться в качестве параметра типа
TImplementation в универсальный тип или метод
'ServiceCollectionServiceExtensions.AddSingleton(IServiceCollection)'. Неявной ссылки
преобразования из MyThingFactory в IThingFactory нет
Непонятно, как это зарегистрировать.
Подробнее здесь: https://stackoverflow.com/questions/783 ... ic-factory