C# 类中的方法

发布于 2024-09-25 09:17:57 字数 704 浏览 1 评论 0原文

我的项目中的类库中有许多不同的类。我使用 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 技术交流群。

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

发布评论

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

评论(6

早茶月光 2024-10-02 09:17:57

不,你不能嵌套方法。您可以使用以下几种方法:

  • 您可以创建匿名函数 在方法内部并以与调用方法类似的方式调用它们。
  • 您可以将一种方法中的局部变量提升为成员字段,然后可以从两种方法访问它们。

No, you can't nest methods. Here are a couple of approaches you can use instead:

  • You can create anonymous functions inside methods and call them in a similar way to calling a method.
  • You can promote local variables in one method to member fields and then you can access them from both methods.
情话难免假 2024-10-02 09:17:57

您似乎误解了类代码的工作原理?

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
    }
}

或者我完全误解了你的问题?

You seem to have misunderstood how class code works?

GetFileList() doesn't execute just because you have placed it in the class after Execute() - you have to actually call it, like this:

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
    }
}

or have i totally misunderstood your question?

决绝 2024-10-02 09:17:57

您可以使用 lambda 表达式:

public virtual void Execute(JobExecutionContext context) 
{ 

    Func<string[]> getFileList = () => { /*access context and return an array */};

    string[] strings = getFileList();

} 

You could use lambda expressions:

public virtual void Execute(JobExecutionContext context) 
{ 

    Func<string[]> getFileList = () => { /*access context and return an array */};

    string[] strings = getFileList();

} 
无边思念无边月 2024-10-02 09:17:57

您是否尝试从 GetFileList 函数获取结果并在 Execute 中使用它们?
如果是这样,那么试试这个:

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[] file_list = GetFileList();
        // use file_list
    }

    private string[] GetFileList()
    { 
       //Code for getting file list
       return list_of_strings;
    }
}

Are you trying to get the results from the GetFileList function and use them in Execute?
If so, then just try this:

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[] file_list = GetFileList();
        // use file_list
    }

    private string[] GetFileList()
    { 
       //Code for getting file list
       return list_of_strings;
    }
}
九命猫 2024-10-02 09:17:57

您似乎想根据某些上下文信息获取文件列表 - 在本例中,只需向 GetFileList 方法添加一个参数并从 Execute 传递它:

public virtual void Execute(JobExecutionContext context)
{
    string[] fileList = this.GetFileList(context);
    ...
}

private string[] GetFileList(JobExecutionContext) { ... }

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 from Execute:

public virtual void Execute(JobExecutionContext context)
{
    string[] fileList = this.GetFileList(context);
    ...
}

private string[] GetFileList(JobExecutionContext) { ... }
小帐篷 2024-10-02 09:17:57

Execute 是一个虚拟方法,它不是声明其他方法的空间,您需要在其中放置作业的任何逻辑,它不是声明新方法的命名空间。如果您想使用方法化逻辑,只需在类定义中声明它们并从执行函数中调用它们即可。

public virtual void Execute(JobExecutionContext context)
{

    mymethod1();
    mymethod2();
}

private void mymethod1()
{}

private void mymethod2()
{}

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.

public virtual void Execute(JobExecutionContext context)
{

    mymethod1();
    mymethod2();
}

private void mymethod1()
{}

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