title: wraper function in C#
body: <p>I need to &quot;wrap&quot; C# methods to set and initialize variables. The wrapper has to do something BEFORE and AFTER the wrapped method. Just as a basic example, I'd like to do something like:</p>
  <pre><code>public void foo()
  {
   Console.WriteLine(&quot;inside foo&quot;);
  }
  
  public void silly_wrapper(string ham)
  {
   // somethig before
   Console.WriteLine(ham + &quot;before&quot; );
   DateTime tic = DateTime.Now; //Current Date
  
   // call function
   foo();
  
   // something after
   TimeSpan time = DateTime.Now - tic; //Current Date
   Console.WriteLine(ham + $&quot;after in {time}&quot;);
  }
  </code></pre>
  <p>I have to repeat this on almost ALL my methods. Actually I want to call an <code>Initialize()</code> method after all the constructors of all the classes in my project.</p>
  <p>Is there any way to do it in a smarter way? I've tried to implement <a href="https://docs.microsoft.com/en-us/dotnet/api/system.attribute" rel="nofollow noreferrer">Attribute</a> but there is no way to call something after the method.</p>
  <pre><code>[mywrapperFunction(&quot;spam&quot;)]
  public void foo()
  {
   Console.WriteLine(&quot;inside foo&quot;);
  } 
  </code></pre>
  <p>Generalizing the wrapper with a <code>Func</code> argument is going to be a mess (too many options) and I'm supposed to call every function inside the wrapper function.</p>