Я пытаюсь настроить внедрение зависимостей в своем приложении .Net 8 и хочу, чтобы оно сканировалось и регистрировалось на основе соглашения об именах. Я застрял в том, что не могу понять, как зарегистрировать IFoo t.GetInterfaces().Any(i => i.IsGenericType)).ToList();
foreach (var type in types)
{
var interfaces = type.GetInterfaces();
foreach (var @interface in interfaces)
{
// Register the interface mapping to the type in DI container
services.AddTransient(@interface, type);
// Register a factory for the Funcs
services.AddTransient(typeof(Func).MakeGenericType(@interface),
provider => new Func(() => provider.GetService(@interface)));
}
}
}
[/code]
Вот пример настройки классов:
Код: Выделить всё
public interface ISpecificFoo : IFoo
{
}
public class SpecificFoo : ISpecificFoo
{
public class Output
{
}
}
System.ArgumentException: 'Cannot instantiate implementation type
'ISpecificFoo'
for service type
'IFoo`2
Источник: https://stackoverflow.com/questions/781 ... etype-type