Why can't I use a variable for 'typeof' in Quartz.NET?
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 di开发者_如何转开发rective 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.
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.
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.
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.
You can get the type of a variable, by using mergedJobType.GetType()
.
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.
精彩评论