title: c# how to call an Action from generic extension method in another class
body: <p>I am trying to write a Library of functions to be used by all the classes in my project ( a little unity game ). This question has possibly been asked already but I'm not sure what to search for and haven't found anything yet, so here I am.
  I want to create a function in a Library that takes in argument a delegate ( or at least, an object of type Action, not sure if that's really called a delegate ) and runs it. The catch is, this Action has to be called by an object of another type and potentially modify it ( meaning it has to be called by the other object ). I'm thinking about using a generic extension method for this.
  Here is some code I have for now( although this code doesn't work, it should be pretty explicit):</p>
  <pre><code>    public static class Library 
  {
      public static void doStuffandCallMethod&lt;T&gt;(this T self, Action methodToExecute) {
          //DoStuff
          self.methodToExecute();
  
      }
      
  }
  </code></pre>
  <p>which is my attempt at generalizing this code:</p>
  <pre><code>public static void WaitAndDo( Action methodToExecute) {
          //DoSuff();
          methodToExecute();
      }
  </code></pre>
  <p>which works fine.
  However, the compilator doesn't understand and look for a method called methodToExecute in the class   T instead of replacing it by the method described by the parameter (Action) methodToExecute.
  How am I supposed to do call this Action as the object self, from the class Lib?</p>
  <p>Big thanks to those who take the time to thing about it ;)</p>
  <p>Edit: Here is a minimal exemple:</p>
  <pre><code>public class myObject {
      int number;
  
      public myObject() {
          number = 0;
      }
  
      public void method1() {
          Console.WriteLine(&quot;Bob is a great guy&quot;);
      }
      public void method2() {
          number++;
      }
  
      public void method3() {
          DoItForMe&lt;myObject&gt;(method2);
      }
  }
  
  public class Lib {
      public static void DoItForMe&lt;T&gt;(this T self, Action methodToExecute) {
          self.methodtoExecute();
      }
  }
  </code></pre>
  <p>Then from the main:</p>
  <pre><code>myObject bob = new myObject();
  bob.method3();
  Console.WriteLine(&quot;bob's number is&quot; + bob.number);
  </code></pre>
  <p>should give the output</p>
  <p>bob's number is 1</p>