C# 类中的方法
我的项目中的类库中有许多不同的类。我使用 Quartz.NET(一个调度系统)来调度和加载作业,实际的作业执行是在这些类库中完成的。我计划有多种类型的作业类型,并且每种类型都会在类库中拥有自己的类来执行。
我遇到的一个问题是我无法在这些类中嵌套方法。例如,这是我的类:
public class FTPtoFTP : IJob
{
private static ILog _log = LogManager.GetLogger(typeof(HTTPtoFTP));
public FTPtoFTP()
{
}
public virtual void Execute(JobExecutionContext context)
{
//Code that executes, the variable context allows me to access the job information
}
}
如果我尝试在类的执行部分中放置一个方法,例如...
string[] GetFileList()
{
//Code for getting file list
}
它期望在我的 GetFileList 方法开始之前结束执行方法,并且也不让我访问我需要的上下文变量。
我希望这是有道理的,再次感谢 - 你们统治
I have a number of different clases located in a class library in my project. I am using Quartz.NET (a scheduling system) to schedule and load jobs, and the actual job execution is done in these class libraries. I plan to have many types of job types, and each one will have their own class for execution in the class library.
An issue I have is that I can't nest methods in these classes. For example, here is my class:
public class FTPtoFTP : IJob
{
private static ILog _log = LogManager.GetLogger(typeof(HTTPtoFTP));
public FTPtoFTP()
{
}
public virtual void Execute(JobExecutionContext context)
{
//Code that executes, the variable context allows me to access the job information
}
}
If I try to put a method in the execution part of the class, such as...
string[] GetFileList()
{
//Code for getting file list
}
It expects the end of the execution method before my GetFileList one begins, and also doesn't let me access the context variable which I need.
I hope this makes sense, thanks again - you guys rule
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
不,你不能嵌套方法。您可以使用以下几种方法:
No, you can't nest methods. Here are a couple of approaches you can use instead:
您似乎误解了类代码的工作原理?
GetFileList()
不会执行只是因为您将它放在Execute()
之后的类中 - 您必须实际调用它,如下所示:或者我完全误解了你的问题?
You seem to have misunderstood how class code works?
GetFileList()
doesn't execute just because you have placed it in the class afterExecute()
- you have to actually call it, like this:or have i totally misunderstood your question?
您可以使用 lambda 表达式:
You could use lambda expressions:
您是否尝试从
GetFileList
函数获取结果并在Execute
中使用它们?如果是这样,那么试试这个:
Are you trying to get the results from the
GetFileList
function and use them inExecute
?If so, then just try this:
您似乎想根据某些上下文信息获取文件列表 - 在本例中,只需向
GetFileList
方法添加一个参数并从Execute
传递它:It seems you want to get the file list based on some context information - in this case just add a parameter to the
GetFileList
method and pass it fromExecute
:Execute
是一个虚拟方法,它不是声明其他方法的空间,您需要在其中放置作业的任何逻辑,它不是声明新方法的命名空间。如果您想使用方法化逻辑,只需在类定义中声明它们并从执行函数中调用它们即可。Execute
is a virtual method, its not a space to declare additional methods, inside you're meant to place any logic for the job, its not a namespace to declare new methods. If you want to use methodised logic, just declare them within the class definition and call them from the execute function.