我需要从Autofac的LifeTimeScope解决一些Quartz作业。目前我已经实施了工厂类:
public class JobWrapper<T> : IJob where T : IJob
在另一个类中,我有返回已解析作业的方法:
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
{
Type type = bundle.JobDetail.JobType;
return _lifetimeScope.Resolve<JobWrapper<type>>(); // does not work
}
我需要将泛型传递给JobWrapper类,目前我还不知道该怎么做。
答案 0 :(得分:3)
您可以自己描述类型
// The type will be the Type for JobWrapper<CleanJob>
var type = typeof(JobWrapper<>).MakeGenericType(typeof(CleanJob));
return (IJob)container.Resolve(type);
// CleanJob decleration
public class CleanJob : IJob
答案 1 :(得分:0)
好吧,我明白了:
public class InjectorJobFactory : IJobFactory
{
private readonly ILifetimeScope _lifetimeScope;
public InjectorJobFactory(ILifetimeScope lifetimeScope)
{
_lifetimeScope = lifetimeScope;
}
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
{
var type = typeof(JobWrapper<>).MakeGenericType(bundle.JobDetail.JobType);
return (IJob)_lifetimeScope.Resolve(type);
}
public void ReturnJob(IJob job)
{
}
}
我刚刚创建了另一个方法,然后只使用了MakeGenericMethod