我在项目的类库中有许多不同的类。我使用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方法开始之前结束执行方法,并且不允许我访问我需要的上下文变量。
我希望这是有意义的,再次感谢-你们说了算
发布于 2010-09-30 19:26:12
你似乎误解了类代码是如何工作的?
GetFileList()不会仅仅因为您在Execute()之后将其放入类中而执行-您必须实际调用它,如下所示:
public class FTPtoFTP : IJob
{
private static ILog _log = LogManager.GetLogger(typeof(HTTPtoFTP));
public FTPtoFTP()
{
}
public virtual void Execute(JobExecutionContext context)
{
string[] files = GetFileList();
//Code that executes, the variable context allows me to access the job information
}
string[] GetFileList()
{
//Code for getting file list
}
}还是我完全误解了你的问题?
发布于 2010-09-30 19:27:22
不,你不能嵌套方法。以下是您可以使用的几种方法:
发布于 2010-09-30 19:26:42
您可以使用lambda表达式:
public virtual void Execute(JobExecutionContext context)
{
Func<string[]> getFileList = () => { /*access context and return an array */};
string[] strings = getFileList();
} https://stackoverflow.com/questions/3830052
复制相似问题