quartz.net中JobBuilder的Create方法中的类实例

时间:2014-06-03 09:43:15

标签: c# quartz.net

JobBuilder的默认创建方法是

IJobDetail paymentJob = JobBuilder.Create<Hello>().WithIdentity(jobName, groupName).Build();

我检查了重载,但没有重载我们可以在里面有一个类的实例。创建方法。像这样的东西

IJobDetail paymentJob = JobBuilder.Create<new Hello()>().WithIdentity(jobName, groupName).Build();

但这会产生错误

  

运营商&lt;不能应用于'方法组'或'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();
        }

    }
}

1 个答案:

答案 0 :(得分:1)

只需更改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/