[英]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.