存储一个接一个地执行所有方法

Storing methods to execute all one after the other

我正在与 SSH.NET 合作,我想在我的 Connection class 中创建 "Secure mode"。基本上,它应该存储在一个连接中完成的所有方法,然后在需要时一个接一个地 "execute" 它们。像这样:

Connection conn = new Connection("server", "username", "password");
conn.secureMode(true); //enabling secure mode
conn.changeDirectory("directory");
conn.downloadFile("file");
conn.FlushMethods();
conn.disconnect();

Flush()之后,changeDirectory("directory")downloadFile("file")都应该是"executed"。

所以我应该将这些方法存储在某种数组或队列中,对吗?但是怎么办?然后怎么执行呢?

你能给我一些提示吗?

在内部,您可以将命令列表存储为 class 中的一系列操作,并且当您想要 "flush" 命令时,仅 运行 此列表。

 public class Connection
 {
     private List<Action> _commandList = new List<Action>();

     public void ChangeDirectory(string directoryName)
     {
         _commandList.Add(() => 
             {
             //Actual code to change directory
             });
     }

     public void FlushMethods()
     {
         foreach(var command in _commandList)
         {
             command();
         }
         _commandList.Clear();
    }
}