Skip to content

Commit

Permalink
Fix ObservableCollection binary serialization (based on 828a6a4)
Browse files Browse the repository at this point in the history
  • Loading branch information
marek-safar committed Jul 30, 2013
1 parent d0cfdc2 commit ebff81b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,30 +40,33 @@ namespace System.Collections.ObjectModel
#endif
public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged {
[Serializable]
sealed class Reentrant : IDisposable {
private int count = 0;
#if !MOBILE
[TypeForwardedFrom (Consts.WindowsBase_3_0)]
#endif
sealed class SimpleMonitor : IDisposable {
private int _busyCount;

public Reentrant()
public SimpleMonitor()
{
}

public void Enter()
{
count++;
_busyCount++;
}

public void Dispose()
{
count--;
_busyCount--;
}

public bool Busy
{
get { return count > 0; }
get { return _busyCount > 0; }
}
}

private Reentrant reentrant = new Reentrant ();
private SimpleMonitor _monitor = new SimpleMonitor ();

public ObservableCollection ()
{
Expand All @@ -83,7 +86,9 @@ public ObservableCollection (List<T> list)
{
}

[field:NonSerialized]
public virtual event NotifyCollectionChangedEventHandler CollectionChanged;
[field:NonSerialized]
protected virtual event PropertyChangedEventHandler PropertyChanged;

event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged {
Expand All @@ -93,16 +98,16 @@ event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged {

protected IDisposable BlockReentrancy ()
{
reentrant.Enter ();
return reentrant;
_monitor.Enter ();
return _monitor;
}

protected void CheckReentrancy ()
{
NotifyCollectionChangedEventHandler eh = CollectionChanged;

// Only have a problem if we have more than one event listener.
if (reentrant.Busy && eh != null && eh.GetInvocationList ().Length > 1)
if (_monitor.Busy && eh != null && eh.GetInvocationList ().Length > 1)
throw new InvalidOperationException ("Cannot modify the collection while reentrancy is blocked.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,25 @@ namespace System.Collections.ObjectModel
public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged {

private class Reentrant : IDisposable {
private int count = 0;
private int _busyCount;

public Reentrant()
{
}

public void Enter()
{
count++;
_busyCount++;
}

public void Dispose()
{
count--;
_busyCount--;
}

public bool Busy
{
get { return count > 0; }
get { return _busyCount > 0; }
}
}

Expand Down

0 comments on commit ebff81b

Please sign in to comment.