views:

60

answers:

5

I'm trying to create a job in an ASP.NET (C#) form using Quartz.NET, and here's what I have so far:

JobDetail jobDetail = new JobDetail(count + "_job", schedID, typeof(HTTPtoFTP));

Problem is, I don't want to link directly to the HTTPtoFTP class, because depending on what the user picks on the form , it'll link to a seperate class. I've tried using a variable in place of HTTPtoFTP, but I get the error:

The type or namespace 'mergedJobType' could not be found (are you missing a using directive or an assembly reference?)

Why is this? I guess one way to do this is an IF statement where I just copy the line and change the typeof for each possibility, but it seems like I'd have to replicate all the other lines that refer to jobDetail too.

Thanks

+2  A: 

Unless I'm missing something, I think what you are looking for is mergedJobType.GetType() That returns the type object of an object's class.

Andrew Barber
Thanks all, now I get 'Job class must implement the Job interface.' :( Type mergedJobType2 = mergedJobType.GetType(); JobDetail jobDetail = new JobDetail(count + "_job", schedID, mergedJobType2);
Chris
A: 

You can get the type of a variable, by using mergedJobType.GetType().

Jappie
+1  A: 

Because that's precisely what typeof does, it takes the label for a type and returns the relevant Type object.

What you would want would be mergedJobType.GetType(). GetType() returns the type of an instance.

Jon Hanna
+2  A: 

In .NET, there are two most common ways to retrieve a type.

When the type is known at compile-time, use typeof.
When the type is only known at runtime and you have a reference to object of that type, call its GetType() to get the Type object.

Note that for generic types there's a difference between, say, typeof(List<int>) and typeof(List<>) so if you're into heavy reflection use, you may want to learn how to deal with generic runtime types.

gaearon
A: 

Everyone's comments so far are correct. I just went through this myself. The following line should do what you need as long as mergedJobType is an instance of a class that implements IJob:

JobDetail jobDetail = new JobDetail(count + "_job", schedID, mergedJobType.GetType());

The error you're getting "Job class must implement the Job interface.' :( Type mergedJobType2 = mergedJobType.GetType(); JobDetail jobDetail = new JobDetail(count + "_job", schedID, mergedJobType2);" is more than likely due to mergedJobType not implementing the IJob interface. All Quartz.Net jobs must implement the IJob interface.

phreak3eb