title: C# method observable
body: <p>I have a method that is called whenever something happens. It can be called any number of times. Then method returns a Task. Basically I want to setup an &quot;obserable&quot; for when ever this method is called. I have no control over the method being called. The method that is called is virtual so I can override it an see that it gets called. But I would rather setup an &quot;observable&quot;. So I have something like</p>
  <pre><code>class Foo
  {
    void Bar()
  }
  </code></pre>
  <p>Again I have no control over Foo but I have an instance of it.</p>
  <pre><code>var instanceofFoo = new Foo();
  </code></pre>
  <p>I want to setup an &quot;observable&quot; that will be triggered anytime that <code>instanceOfFoo.Bar</code> is called.</p>
  <p>The reason that I want an &quot;observable&quot; is because I don't want to have to setup the &quot;observer&quot; again for possibly the next call. I am coding in C# and I looked at <code>IObservable&lt;T&gt;</code> and <code>IObserver&lt;T&gt;</code> but there it specifically indicated that the &quot;provider&quot; had to implement <code>IObservable&lt;T&gt;</code>. Since I don't have control over the provider (in this case the instance of Foo) I cannot use this. I looked at Rx.NET but was unable to glean from the examples something that fit this situation. To further complicate things assume that the instance of Foo is a dependency of another class like</p>
  <pre><code>class FooService
  {
      public bool wasCalled { get; set; }
      public FooService(Foo f)
      {
         // TODO: Setup code to trigger when f.Bar() is called
      }
  }
  </code></pre>
  <p>This setup would need to be able to be cleanly torn down so that there are no dangling &quot;subscribers&quot;.</p>
  <p>So the flow would be something like this</p>
  <ol>
  <li>var foo = new Foo();</li>
  <li>var fooService = new FooService(foo);</li>
  <li>foo.Bar();</li>
  <li>Assert.IsTrue(fooService.wasCalled);</li>
  </ol>
  <p>Ideas?</p>