为什么我不能将变量用于“typeof”在 Quartz.NET 中?

发布于 2024-09-26 00:29:47 字数 442 浏览 3 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

乖乖公主 2024-10-03 00:29:47

除非我遗漏了一些东西,否则我认为您正在寻找的是 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.

神魇的王 2024-10-03 00:29:47

在 .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>) and typeof(List<>) so if you're into heavy reflection use, you may want to learn how to deal with generic runtime types.

习ぎ惯性依靠 2024-10-03 00:29:47

因为这正是 typeof 的作用,它采用类型标签并返回相关的 Type 对象。

您想要的是 mergedJobType.GetType()GetType() 返回实例的类型。

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.

GRAY°灰色天空 2024-10-03 00:29:47

您可以使用 mergedJobType.GetType() 获取变量的类型。

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

爱冒险 2024-10-03 00:29:47

到目前为止,每个人的评论都是正确的。我自己刚刚经历过这个。只要 mergedJobType 是实现 IJob 的类的实例,以下行就应该满足您的需要:

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

您收到的错误“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:

 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文