为什么我不能将变量用于“typeof”在 Quartz.NET 中?
我正在尝试使用 Quartz.NET 以 ASP.NET (C#) 形式创建一个作业,到目前为止,这是我所拥有的:
JobDetail jobDetail = new JobDetail(count + "_job", schedID, typeof(HTTPtoFTP));
问题是,我不想直接链接到HTTPtoFTP 类,因为根据用户在表单上选择的内容,它将链接到一个单独的类。我尝试使用变量代替 HTTPtoFTP,但收到错误:
找不到类型或命名空间“mergedJobType”(是否缺少 using 指令或程序集引用?)
这是为什么?我想做到这一点的一种方法是使用 IF 语句,我只需复制该行并更改每种可能性的 typeof,但似乎我也必须复制引用 jobDetail 的所有其他行。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
除非我遗漏了一些东西,否则我认为您正在寻找的是
mergedJobType.GetType()
它返回对象类的类型对象。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.在 .NET 中,有两种最常见的方法来检索类型。
当类型在编译时已知时,使用
typeof
。当类型仅在运行时已知并且您拥有对该类型的对象的引用时,调用其
GetType()
来获取 Type 对象。请注意,对于泛型类型,
typeof(List)
和typeof(List<>)
之间存在差异,因此如果您需要大量反射,请使用,您可能想了解如何处理通用运行时类型。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>)
andtypeof(List<>)
so if you're into heavy reflection use, you may want to learn how to deal with generic runtime types.因为这正是
typeof
的作用,它采用类型标签并返回相关的Type
对象。您想要的是
mergedJobType.GetType()
。GetType()
返回实例的类型。Because that's precisely what
typeof
does, it takes the label for a type and returns the relevantType
object.What you would want would be
mergedJobType.GetType()
.GetType()
returns the type of an instance.您可以使用
mergedJobType.GetType()
获取变量的类型。You can get the type of a variable, by using
mergedJobType.GetType()
.到目前为止,每个人的评论都是正确的。我自己刚刚经历过这个。只要 mergedJobType 是实现 IJob 的类的实例,以下行就应该满足您的需要:
您收到的错误“Job 类必须实现 Job 接口。” :(
Type mergedJobType2 = mergedJobType.GetType(); JobDetail jobDetail = new JobDetail(count + "_job", schedID, mergedJobType2);
很可能是由于 mergedJobType 未实现 IJob 接口。所有 Quartz .Net 作业必须实现 IJob 接口。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:
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.