title: How do I make a generic wrapper method that returns the wrapped method's result, if it has one?
body: <p>I'm trying to make a method that accepts another method, and returns the value that the inner method returns, if that inner method isn't void.  I'd like to do it without differentiating between <code>Func&lt;&gt;</code> and <code>Action&lt;&gt;</code>.</p>
  
  <p>Essentially, I want the wrapped method to behave the exact same way as the unwrapped method, plus some functionality provided by the wrapper.  The goal is simple enough, but it's hard to wrap my head around the implementation.</p>
  
  <pre class="lang-cs prettyprint-override"><code>public int ReturnsInteger() {
      Console.WriteLine("I return 42");
      return 42;
  }
  
  public static T WrapperMethod(Func&lt;T&gt; someMethod) {
      Console.WriteLine("Wrap start");
      var result = someMethod();
      Console.WriteLine("Wrap end");
      return result;
  }
  
  private static void Main() {
      var X = WrapperMethod(()=&gt;ReturnsInt());
      Console.WriteLine("X = " + X);
      // Wrap start
      // I return 42
      // Wrap end
      // X = 42
  }
  </code></pre>
  
  <pre class="lang-cs prettyprint-override"><code>public void ReturnsNothing() {
      Console.WriteLine("I return nothing");
      return;
  }
  
  public static T WrapperMethod(Action someMethod) {
      Console.WriteLine("Wrap start");
      someMethod();
      Console.WriteLine("Wrap end");
  }
  
  private static void Main() {
      WrapperMethod(()=&gt;ReturnsNothing());
      // Wrap start
      // I return nothing
      // Wrap end
  }
  </code></pre>