Im sure this must be possibly, but it is beyond me at present.
I have a method that initialises several jobs, based around quartz engine. for example
private void InitialiseJobs(IScheduler scheduler)
{
var ns = "Proto.QuartzScheduler.Domain.Jobs";
var classname = "ExampleJob";
var jobSetup = new JobSetup<type>(scheduler);
jobSetup.Run(10);
var second_jobSetup = new JobSetup<ExampleJob>(scheduler);
second_jobSetup.Run(20);
}
The last two lines of this work. I want to be able to define the type via the namespace and class name and pass in into the generic type. e.g replace ExampleJob
with whatever I define.
Any help appreciated.
Apologies I should have given you all a bit more than prevent the suggestion below from working:
public class JobSetup<TType> where TType : IJobToDo
{
private readonly IScheduler scheduler;
private readonly IJobDetail jobDetail;
private readonly JobKey jobKey;
public JobSetup(IScheduler scheduler)
{
this.scheduler = scheduler;
this.jobKey = new JobKey(Guid.NewGuid().ToString(), typeof(TType).ToString());
this.jobDetail = JobBuilder
.Create<JobRunner<TType>>()
.WithIdentity(this.jobKey)
.Build();
}
public void Set<TPropertyType>(Expression<Func<TType, TPropertyType>> expression, TPropertyType value)
{
var memberExpression = expression.Body as MemberExpression;
if (memberExpression == null)
{
throw new ArgumentException("Could not convert the expression to a member expression.");
}
this.jobDetail.JobDataMap[memberExpression.Member.Name] = value;
}
public TPropertyType Get<TPropertyType>(Expression<Func<TType, TPropertyType>> expression)
{
var memberExpression = expression.Body as MemberExpression;
if (memberExpression == null)
{
throw new ArgumentException("Could not convert the expression to a member expression.");
}
return (TPropertyType)this.jobDetail.JobDataMap.Get(memberExpression.Member.Name);
}
public void Run(int number_of_seconds)
{
var trigger = new SimpleTriggerImpl
(
this.jobKey.Name,
this.jobKey.Group,
DateTime.UtcNow,
null,
SimpleTriggerImpl.RepeatIndefinitely,
TimeSpan.FromSeconds(number_of_seconds)
);
this.scheduler.ScheduleJob(this.jobDetail, trigger);
}
}
And the interface:
public interface IJobToDo
{
void Run();
}
And the ExampleJob
public class ExampleJob : IJobToDo
{
public void Run()
{
Console.WriteLine("HelloJob is executing. {0}", System.DateTime.Now.ToUniversalTime());
}
}
I dont have a parameterless constructor for the class being activated?
Keep the suggestions comming please
Cheers