简体   繁体   English

C#中的类中的方法

[英]Methods in classes in C#

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. 我正在使用Quartz.NET(调度系统)来调度和加载作业,而实际的作业执行是在这些类库中完成的。 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. 它期望在我的GetFileList开始之前执行方法的结束,并且也不允许我访问所需的上下文变量。

I hope this makes sense, thanks again - you guys rule 我希望这是有道理的,再次感谢-你们统治

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. 您可以在一种方法中将局部变量提升为成员字段,然后可以从这两种方法中访问它们。

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: GetFileList()不会仅仅因为将它放在Execute()之后的类中而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
    }
}

or have i totally misunderstood your question? 还是我完全误解了您的问题?

You could use lambda expressions: 您可以使用lambda表达式:

public virtual void Execute(JobExecutionContext context) 
{ 

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

    string[] strings = getFileList();

} 

Are you trying to get the results from the GetFileList function and use them in Execute ? 您是否要从GetFileList函数获取结果并在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;
    }
}

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. Execute是一个虚拟方法,它不是用于声明其他方法的空间,它是在内部放置作业的任何逻辑,而不是用于声明新方法的名称空间。 If you want to use methodised logic, just declare them within the class definition and call them from the execute function. 如果要使用方法化的逻辑,只需在类定义中声明它们,然后从execute函数调用它们即可。

public virtual void Execute(JobExecutionContext context)
{

    mymethod1();
    mymethod2();
}

private void mymethod1()
{}

private void mymethod2()
{}

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 : 似乎您想基于一些上下文信息获取文件列表-在这种情况下,只需将参数添加到GetFileList方法并从Execute传递它即可:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM