title: How to intercept a call to a nonvirtual method from/to thirdy-party libraries in .Net?
body: <p>I think what I need is something the .net folks call "transparent dynamic proxy", but all the implementations I've seen this far (Castle DynamicProxy, Spring.NET AOP, etc) require me to do at least one of these:</p>
  
  <ol>
  <li>Declare intercepted method as virtual</li>
  <li>Wrap class and create instances of the wrapper instead of wrapped class</li>
  <li>Change inheritance or implement interfaces</li>
  </ol>
  
  <p>Obviously, if both caller and callee are nonvirtual and from thirdy-party closed source libraries, which is the case, there is nothing I can do.</p>
  
  <p>If C# were a dynamic language like Python I would do something like this:</p>
  
  <pre><code>foo = ThirdyPartyLibA.Foo()
  def interceptor(self, *args, **kwargs):
      do_something_before(self, *args, **kwargs)
      result = ThirdyPartyLibB.Bar.intercepted(self, *args, **kwargs)
      do_something_after(self, result, *args, **kwargs)
      return result
  foo.bar.intercepted = interceptor # bar is an instance of ThirdyPartyLibB.Bar
  foo.do_its_job() # Foo.do_its_job calls Bar.intercepted
  </code></pre>
  
  <p>I need this to change a bad behavior of ThirdyPartyLibA.Foo while interacting with ThirdyPartyLibB.Bar. I know exactly what causes this behavior and exactly how to change Foo or Bar to fix this bug thanks to dissasemblers.</p>
  
  <p>Some (very unlikely to work) ideas:</p>
  
  <ul>
  <li>Disassemble ThirdyPartyLibA, make changes in code and generate a compatible assembly (unlikely to work because it's a strong-named assembly)</li>
  <li>Edit binary to make Foo's buggy methods virtual and change whatever is necessary for it to remain a valid assembly so I can use dynamic proxies (very unlikely to work, also because of the same reason as the idea above)</li>
  <li>Find a transparent dynamic proxy implementation that fits (I think there is none based on this forum thread: <a href="http://www.pcreview.co.uk/forums/overriding-non-virtual-methods-using-il-and-reflection-emit-t2605695.html" rel="nofollow">http://www.pcreview.co.uk/forums/overriding-non-virtual-methods-using-il-and-reflection-emit-t2605695.html</a>)</li>
  <li>Contact the company that created the library (they don't support the product anymore)</li>
  <li>Stop using the library or use an alternative (impossible, since it's part of the runtime of a RAD IDE that we are tied to because there is a HUGE amount of code written using the IDE own language)</li>
  <li>Control the calls to the problematic methods to avoid the bug (we already did this but it didn't solve the problem completely)</li>
  </ul>
  
  <p>Do you have any other idea?</p>
  
  <p>PS: Sorry for my bad english. Also, sorry for my Python. This code is here just to illustrate what I need, don't take it as a recipe because it's horrible.</p>