title: Creating an INotifyPropertyChanged proxy to dispatch calls to UI thread
body: <p>I would like to create a dynamic proxy for binding WinForms controls to objects changed by a different (non-GUI) thread. Such a proxy would intercept the PropertyChanged event and dispatch it using the proper SynchronizationContext.</p>
  
  <p>That way I could use a helper class to do the job, without having to implement the synchronization manually every time (<code>if (control.InvokeRequired) etc.</code>).</p>
  
  <p>Is there a way to do that using LinFu, Castle or a similar library?</p>
  
  <p><strong>[Edit]</strong></p>
  
  <p>Data source is not necessarily a list. It can be any business object, e.g.:</p>
  
  <pre><code>interface IConnection : INotifyPropertyChanged
  {
      ConnectionStatus Status { get; }
  }
  </code></pre>
  
  <p>I could create a wrapper which could do the job, and it would look something like this:</p>
  
  <pre><code>public class ConnectionWrapper : IConnection
  {
       private readonly SynchronizationContext _ctx;
       private readonly IConnection _actual;
       public ConnectionWrapper(IConnection actual)
       {
           _ctx = SynchronizationContext.Current;
           _actual= actual;
           _actual.PropertyChanged += 
              new PropertyChangedEventHandler(actual_PropertyChanged);
       }
  
       // we have to do 2 things:
       // 1. wrap each property manually
       // 2. handle the source event and fire it on the GUI thread
  
       private void PropertyChanged(object sender, PropertyChangedEvArgs e)
       {
           // we will send the same event args to the GUI thread
           _ctx.Send(delegate { this.PropertyChanged(sender, e); }, null);
       }
  
       public ConnectionStatus Status 
       { get { return _instance.Status; } }
  
       public event PropertyChangedEventHandler PropertyChanged;
  }
  </code></pre>
  
  <p>(there may be some errors in this code, I am making it up)</p>
  
  <p>What I would like to do is to have a dynamic proxy (Reflection.Emit) one liner for this, e.g.</p>
  
  <pre><code>IConnection syncConnection
        = new SyncPropertyChangedProxy&lt;IConnection&gt;(actualConnection);
  </code></pre>
  
  <p>and I wanted to know if something like this was possible using existing dynamic proxy implementations.</p>
  
  <p>A more general question would be: <strong>How to intercept an event when creating a dynamic proxy?</strong> Intercepting (overriding) properties is explained well in all implementations.</p>
  
  <p><strong>[Edit2]</strong></p>
  
  <p>The reason (I think) I need a proxy is that the stack trace looks like this:</p>
  
  <pre>
  at PropertyManager.OnCurrentChanged(System.EventArgs e)
  at BindToObject.PropValueChanged(object sender, EventArgs e)
  at PropertyDescriptor.OnValueChanged(object component, EventArgs e) 
  at ReflectPropertyDescriptor.OnValueChanged(object component, EventArgs e)
  at ReflectPropertyDescriptor.OnINotifyPropertyChanged(object component,
       PropertyChangedEventArgs e)    
  at MyObject.OnPropertyChanged(string propertyName)
  </pre>
  
  <p>You can see that <code>BindToObject.PropValueChanged</code> does not pass the <code>sender</code> instance to the <code>PropertyManager</code>, and Reflector shows that sender object is not referenced anywhere. In other words, when the <code>PropertyChanged</code> event is triggered, component will use reflection to access the property of the <strong>original (bound) data source</strong>.</p>
  
  <p>If I wrapped my object in a class containing only the event (as <a href="https://stackoverflow.com/questions/2135227/creating-an-inotifypropertychanged-proxy-to-dispatch-calls-to-ui-thread/2137309#2137309">Sam</a> proposed), such wrapper class would not contain any properties which could be accessed through Reflection.</p>