title: How can I change a method assigned on an event?
body: <p>I am getting all events, with a certain attribute, and I want to modify these events adding a call to another method.</p>
  
  <pre><code>var type = GetType();
  var events = type.GetEvents().Where(e =&gt; e.GetCustomAttributes(typeof(ExecuteAttribute), false).Length &gt; 0);
  
  foreach (var e in events)
  {
      var fi = type.GetField(e.Name, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField);
      var d = (Delegate)fi.GetValue(this);
      var methods = d.GetInvocationList();
      foreach (var m in methods)
      {
          var args = e.EventHandlerType.GetMethod("Invoke").GetParameters().Select(p =&gt; Expression.Parameter(p.ParameterType, "p")).ToArray();
          var body = m.Method.GetMethodBody();
  
          /**
  
          TODO:
  
          Create a new method with the body of the previous
          and add a call to another method
  
          Remove current method
          Add the new created method
  
          **/
      }
  }
  </code></pre>
  
  <p>What I want is basically what is commented above. "Modify" the subscribed methods of the event. I guess I can't subscribe to it, because I need to pass the parameters the method is passing to the big handler (the new method).</p>
  
  <p>One more example based <a href="https://stackoverflow.com/questions/2441672/how-can-i-dynamically-inject-code-into-event-handlers-in-delphi">on this question</a>. I want to convert this:</p>
  
  <pre><code>var e += (x) =&gt;
  {
      var y = x;
  };
  </code></pre>
  
  <p>To something like this:</p>
  
  <pre><code>var e += (x) =&gt;
  {
      var y = x;
  
      BigHandler(x); // injected code
  };
  </code></pre>
  
  <p>Or this:</p>
  
  <pre><code>var e += (x) =&gt; // new method
  {
      previousE(x); // previous method
  
      BigHandler(x); // additional code
  }
  </code></pre>
  
  <p>How can I do it?</p>
  
  <hr>
  
  <p>The bigger goal:</p>
  
  <p>I need to "detect" when an event is fired and call a method. I also need to send the parameters it is using.</p>
  
  <p>So I can make something like:</p>
  
  <pre><code>public delegate void OnPostSaved(Post p);
  [Execute]
  public event OnPostSaved PostSaved;
  
  public void Save()
  {
      /* save stuff */
      // assume that there is already an event subscribed
      PostSaved(post);
  }
  </code></pre>
  
  <p>Then on my handler method I can check if an event from <code>XYZ</code> was fired, check which event was fired, retrieve the parameter and do something. For example:</p>
  
  <pre><code>public void BigHandler(string eventName, params object[] p)
  {
      if (eventName == "PostSaved")
      {
          var post = p[0] as Post;
          MessageBoard.Save("User posted on the blog: " + post.Content);
      }
  }
  </code></pre>
  
  <p>I know it can be achieved using <code>PostSharp</code>, but I can't use it. I need another solution.</p>
  
  <hr>
  
  <p><strong>Related</strong></p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/3645410/c-reflection-changing-a-methods-body">C# Reflection, changing a method's body</a></li>
  <li><a href="https://stackoverflow.com/questions/2441672/how-can-i-dynamically-inject-code-into-event-handlers-in-delphi">How can I dynamically inject code into event handlers in Delphi?</a></li>
  </ul>
  
  <hr>
  
  <p><strong>Update 2010-09-27</strong> I couldn't find a solution neither more info on it, I still need help. Added +150 bounty.</p>