title: Unsubscribing from, or checking for, the CollectionChanged event via reflection
body: <p>I have used reflection in my project to add events to <code>ObservableCollections</code> - specifically, <code>CollectionChanged</code>. I am able to add the event well enough and there is no problem there. The problem is that the code to add the event is run more than once and so I end up with multiple identical events added in some cases. What I don't know how to do is either check to see if there is already an event assigned, or to remove before I add one (to ensure there is only one event added).</p>
  
  <p>The code is here:</p>
  
  <pre><code>foreach (PropertyInfo propertyInfo in props)
  {
      nType = this.GetPropertyInfoTypeObs(propertyInfo.PropertyType);
  
      if (nType == Static_Enums.TypeEnums.TYPE_OBSERVABLE_COLLECTION)
      {
          var genargs = propertyInfo.PropertyType.GetGenericArguments();
  
          var o = propertyInfo.GetValue(this, null);
  
          EventInfo evi = o.GetType().GetEvent("CollectionChanged", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
          var eventHandler = new Action&lt;object, NotifyCollectionChangedEventArgs&gt;(
                      (s, a) =&gt;
                      {
                          // Event code here
                          this.MakePropertyDirty(propertyInfo.Name);
                      });
  
          var del = Delegate.CreateDelegate(evi.EventHandlerType, eventHandler.Target, eventHandler.Method);
  
          evi.AddEventHandler(o, del);
      }
  }
  </code></pre>
  
  <p>As stated, this part works fine. But how to handle the issue of the event being added more than once? </p>
  
  <p>I first tried simply having <code>evi.RemoveEventHandler(o, del)</code> on the line above. This doesn't work for me - as in, if I watch what happens when that line is executed, the event handler seems to remain untouched. Perhaps I'm doing it incorrectly?</p>
  
  <p>I'd also be happy with a way to check if the event handler already has one assigned, and then skip over adding it again. But how to do that I've no idea.</p>
  
  <p><strong>Edit</strong></p>
  
  <p>I have tried creating the delegate as a class property in order that it can persist. But even still, as I trace over the lines, I can see that the handler is not removed when I call the <code>evi.RemoveEventHandler</code> with this property delegate.</p>
  
  <p>I then went a little further and tried doing this:</p>
  
  <pre><code>EventInfo ef = o.GetType().GetEvent("CollectionChanged");
  MethodInfo rem = ef.GetRemoveMethod();
  ParameterInfo[] piList = rem.GetParameters();
  
  object[] oList = new object[1];
  oList[0] = storedProc;
  
  rem.Invoke(o, oList);
  </code></pre>
  
  <p>Unfortunately it also doesn't seem to do the trick.</p>
  
  <p>Furthermore, I read that in order to remove a handler, it has to be the <em>exact same</em> handler in the remove method. Now, in this method here, the derived class has more than one <code>ObservableCollection</code>. So I tried limiting the creation of the new delegate (and assigning it to the class property) to only in the case of a particularly named <code>ObservableCollection</code> (one I knew would appear). I traced it over again, one line at a time, and the event handler was not removed.</p>
  
  <p>Even if this did work, however, I'd then have to store a delegate for each <code>ObservableCollection</code> for which I did this. My god, can't I just tell it to clear the event?</p>