简体   繁体   中英

Instance of a class in Create method of JobBuilder in quartz.net

The default create method of JobBuilder is

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

I checked the overloads but there is no overload in which we can have an instance of a class inside. the create method. Something like this

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

but this gives an error

Operator < cannot be applied to the 'method group' or 'Hello'

The reason i need this is:

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();
        }

    }
}

Just change your HelloFactory to return a Type object instead of a Hello object:

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;
        }

    }
}

Alternatively, implement your own JobFactory. Here's an exmaple on how to implement one: http://jayvilalta.com/blog/2012/07/23/creating-a-custom-quartz-net-jobfactory/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM