简体   繁体   English

无论如何在.net核心中使用带有反射的ServiceProvider.GetService()?

[英]is there anyway to use ServiceProvider.GetService() with reflection in .net core?

Here is my class.这是我的课。

public class DemoJob : IJob
{
    private readonly ILogger logger;
    public DemoJob(ILogger<SecondJob> logger)
    {
        this.logger = logger;
    }

    public async Task Execute(IJobExecutionContext context)
    {
        await Task.Run(() =>
        {
            Console.WriteLine(context.JobDetail.JobType.FullName);
            logger.LogError(context.JobDetail.JobType.FullName);
        });
    }
}

I want to get all service of one class's dependency injection.我想获得一个类的依赖注入的所有服务。 I could use like我可以用像

var service = serviceProvider.GetService(typeof(DemoJob))

it seems all right.似乎没问题。 But now I want to use it with reflection,like但现在我想用反射来使用它,比如

var jobType = assembly.GetType("xxxx.SecondJob")
var service = serviceProvider.GetService(jobType)

service always returns a null value. service始终返回空值。 Is there anyway?有吗?

在这种情况下,您可以使用 Type.GetType() 而不是 someType.GetType()。帮我解决了这个问题。感谢@KevinGosse

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .Net Core 2.0控制台程序在运行`serviceProvider.GetService&#39;时具有空值 <Application> ();`? - .Net core 2.0 console program got null value when running `serviceProvider.GetService<Application>();`? ServiceProvider.GetService() 显示错误 - ServiceProvider.GetService() showing error 服务提供者.GetService<type> 每次使用都抛出异常</type> - ServiceProvider.GetService<Type> throw exception for every usage ServiceProvider.GetService 为瞬态实例工厂中的作用域服务返回相同的实例 - ServiceProvider.GetService returns the same instance for scoped service in transient instance factory 由于 serviceProvider.GetService,无法添加 singleton 服务<irabbitmqconsumer> () 返回 null</irabbitmqconsumer> - Unable to add a singleton service due to serviceProvider.GetService<IRabbitMQConsumer>() returning null 反正在.NET Core中使用Entity Framework吗? - Is there anyway to use Entity Framework in .NET Core? Mocking ASP Net Core GetService<t></t> - Mocking ASP Net Core GetService<T> 如果我使用ServiceProvider接口解析.NET Core中的属性以进行依赖注入,这是否不好? - Is it bad If I use the ServiceProvider interface to resolve my properties in .NET Core for Dependency Injection 如何在.net core中动态注入IServiceProvide.GetService中的泛型类型 - How to inject the generic type in IServiceProvide.GetService dynamically in .net core .net core 注册通用接口并使用 getservice 检索它们 - .net core register generic interface and retrieve them using getservice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM