title: How to properly refactor some copy/pasted code
body: <p>I am building a command-line exe that can apply several operations on a PDF file (add text, images, resize, crop, etc).</p>
  
  <p>Currently, my Program.cs looks a bit like this (it uses <a href="https://github.com/gsscoder/commandline/" rel="nofollow">CommandLineParser</a>):</p>
  
  <pre><code>switch (invokedVerb)
  {
      case "barcode":
          Operations.BarcodeOperations.AddBarCode(options.AddBarcodeVerb);
          break;
  
      case "addblankpage":
          Operations.PageOperations.AddBlankPage(options.AddBlankPageVerb);
          break;
  }
  </code></pre>
  
  <p>As you can see, I have split the operations into several XXXOperations classes, each of them having very similar instructions:</p>
  
  <pre><code>public static void AddStuff(StuffOptions options)
  {
      Logging.Log("here is a bunch of logging");
  
      // here sometimes there is some action-specific code but not often
  
      using (DocWrapper doc = new DocWrapper(options.File)) // this is in all actions
      {
          foreach (int page in doc.GetPagesToModify(options.Pages)) // this is in most actions
          {
              // call some stuff on the doc instance
          }
  
          doc.Save(options.OutputFile); // this is in all actions
      }
  }
  </code></pre>
  
  <p>So, all actions create a new instance of DocWrapper, most of them loop on its pages (but I could modify the actions so that all of them do), and all of them save, but each of them do a different set of actions inside it.</p>
  
  <p>How could I refactor this code so that the DocWrapper instantiation, the pages loop and the save are in a single place, but I can specify custom code inside the loop ?</p>
  
  <p>I'm thinking of using delegates or Actions to define my actions, but I have no idea where to start, since I'm not very familiar with them.</p>
  
  <p>Thanks!</p>