Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

226
Visualizações
How does the Func delegate work when it requires an interface as a parameter?

Specifically, I'm looking at this line of code from Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.cs:

public static IServiceCollection AddScoped<TService>(this IServiceCollection services, Func<IServiceProvider, TService> implementationFactory) where TService : class;

An example using this method would be:

services.AddScoped<ICustomService>(sp => new CustomService(
    sp.GetRequiredService<IAnotherCustomService>(), "Param1", "Param2"));

I understand how the Func delegate and lambda expressions work, but I'm not understanding how IServiceProvider is being initialized behind the scenes.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

IServiceProvider is not being initialized behind the scenes at this point in time. The framework is merely capturing the passed-in delegate and saving it for a later time, when it has an IServiceProvider instance and needs to generate an ICustomService.

There is nothing specific to interfaces happening here. The same principle would apply with any argument type for a delegate.

// This captures the delegate in a variable
Func<int, string> f = i => i.ToString();

// This invokes the delegate with an instance of an `int`
f(1);
f(2);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda