title: DynamicProxy2: CreateClassProxyWithTarget + IInterceptor
body: <p>If I've missed this in another question I apologize; I looked for a good while before deciding I had a unique question... I want to use DynamicProxy2 to provide interception for a WPF application's model classes.  This is so that I do not have to fully implement INotifyPropertyChanged everywhere.  For instance, the below class should fully participate in two-way data binding once proxied and intercepted:</p>
  
  <pre><code>public class ModelExample : INotifyPropertyChanged
  {
      public event PropertyChangedEventHandler PropertyChanged;
  
      public int Id{ get; set; }
      public string Uri{ get; set; }
      public string Name{ get; set; }
  }
  </code></pre>
  
  <p>I have found that I can create a new instance of the model class and intercept calls to it by calling the CreateClassProxy method: </p>
  
  <pre><code>new ProxyGenerator().CreateClassProxy&lt;T&gt;(interceptors);
  </code></pre>
  
  <p>Unfortunately, this forces me to allow the <code>ProxyGenerator</code> class to create my model instances, and I'm getting those back from my middle tier, i.e. they already exist.  I need to wrap existing objects, so I <strong>think</strong> I need to call <code>CreateClassProxyWithTarget</code> instead:</p>
  
  <pre><code>new ProxyGenerator().CreateClassProxyWithTarget(instance, interceptors);
  </code></pre>
  
  <p>However, when I do that my interceptor stops functioning.  I'm pretty sure it's not the interceptor's fault... it's a very simple object.  Here's its interface:</p>
  
  <pre><code>public interface IFluentInterceptor : IInterceptor
  {
      IFluentInterceptor Before(Action&lt;IInvocation&gt; before);
      IFluentInterceptor After(Action&lt;IInvocation&gt; after);
      IFluentInterceptor Finally(Action&lt;IInvocation&gt; @finally);
      IFluentInterceptor RunCondition(Func&lt;IInvocation, bool&gt; runCondition);
      IFluentInterceptor OnError(Func&lt;IInvocation, Exception, bool&gt; onError);
  }
  </code></pre>
  
  <p>The <code>FluentInterceptor</code> type implements this.  The <code>Before</code>, <code>After</code>, etc. methods are too simple to show; they all add to action queues that are meant to be used during method invocation, then each method returns <code>this</code>, allowing for method chaining.</p>
  
  <p>The below code doesn't work, but I can't figure out why:</p>
  
  <pre><code>new ProxyGenerator().CreateClassProxyWithTarget(instance, new FluentInterceptor()
      .After(invocation =&gt;
      {
          if (!invocation.Method.Name.StartsWith("set_")) return;
          string propertyName = invocation.Method.Name.Substring(4);
          FieldInfo info = invocation.TargetType.GetFields(BindingFlags.Instance | BindingFlags.NonPublic)
              .Where(f =&gt; f.FieldType.Equals(typeof (PropertyChangedEventHandler)))
              .FirstOrDefault();
          if (info == null) return;
          var handler = info.GetValue(invocation.InvocationTarget) as PropertyChangedEventHandler;
          if (handler != null) handler.Invoke(invocation.TargetType, new PropertyChangedEventArgs(propertyName));
      }));
  </code></pre>
  
  <p>If I try that with <code>CreateClassProxy</code>, it works like a charm.  Does anybody see what I'm doing wrong?</p>
  
  <p>Thanks!</p>