在quartz.net的JobBuilder的Create方法中的类的实例
本文关键字:实例 方法 JobBuilder quartz net Create | 更新日期: 2023-09-27 18:06:16
JobBuilder默认的创建方法是
IJobDetail paymentJob = JobBuilder.Create<Hello>().WithIdentity(jobName, groupName).Build();
我检查了重载,但是没有重载,我们可以在里面有一个类的实例。create方法。像这样
IJobDetail paymentJob = JobBuilder.Create<new Hello()>().WithIdentity(jobName, groupName).Build();
但是这会给出一个错误
操作符& lt;不能应用于'method group'或'Hello'
我需要这个的原因是:
public abstract class Hello:IJob
{
public abstract void Execute(IJobExecutionContext context);
}
public Hello1: Hello
{
public void Execute(IJobExecutionContext context)
{
//implementation
}
}
public Hello2: Hello
{
public void Execute(IJobExecutionContext context)
{
//implementation
}
}
public static HelloFactory
{
public Hello GetHelloType(HelloEnum enum)
{
Hello job = new Hello();
switch(enum)
{
case HelloEnum.Type1: job = new Hello1();
case HelloEnum.Type2: job = new Hello2();
}
}
}
只需将HelloFactory更改为返回Type对象而不是Hello对象:
public static class HelloFactory
{
public Type GetHelloType(HelloEnum theEnum)
{
Type type;
switch (theEnum)
{
case HelloEnum.Type1:
type = typeof(Hello1);
break;
case HelloEnum.Type2: job = typeof(Hello2);
break;
}
}
}
或者,实现您自己的JobFactory。这里有一个如何实现的例子:http://jayvilalta.com/blog/2012/07/23/creating-a-custom-quartz-net-jobfactory/