Skip to content

Commit

Permalink
Refactor BindingContext to use Dictionary<TKey, TValue> instead of H…
Browse files Browse the repository at this point in the history
…ashtable and List<T> instead of ArrayList
  • Loading branch information
Jericho committed Nov 23, 2022
1 parent 42c26cc commit 93a34d1
Showing 1 changed file with 9 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ namespace System.Windows.Forms
[DefaultEvent(nameof(CollectionChanged))]
public partial class BindingContext : ICollection
{
private readonly Hashtable _listManagers;
private readonly Dictionary<HashKey, WeakReference> _listManagers;

/// <summary>
/// Initializes a new instance of the System.Windows.Forms.BindingContext class.
/// </summary>
public BindingContext()
{
_listManagers = new Hashtable();
_listManagers = new();
}

/// <summary>
Expand All @@ -44,7 +44,7 @@ int ICollection.Count
void ICollection.CopyTo(Array ar, int index)
{
ScrubWeakRefs();
_listManagers.CopyTo(ar, index);
((ICollection)_listManagers).CopyTo(ar, index);
}

/// <summary>
Expand Down Expand Up @@ -231,8 +231,7 @@ private BindingManagerBase EnsureListManager(object dataSource, string? dataMemb

// Check for previously created binding manager
HashKey key = GetKey(dataSource, dataMember);
WeakReference? wRef = _listManagers[key] as WeakReference;
if (wRef is not null)
if (_listManagers.TryGetValue(key, out WeakReference wRef) && wRef is not null)
{
bindingManagerBase = (BindingManagerBase?)wRef.Target;
}
Expand Down Expand Up @@ -327,21 +326,20 @@ private static void CheckPropertyBindingCycles(BindingContext newBindingContext,

private void ScrubWeakRefs()
{
ArrayList? cleanupList = null;
foreach (DictionaryEntry de in _listManagers)
List<HashKey> cleanupList = null;
foreach (KeyValuePair<HashKey, WeakReference> de in _listManagers)
{
WeakReference wRef = (WeakReference)de.Value!;
if (wRef.Target is null)
if (de.Value.Target is null)
{
cleanupList ??= new ArrayList();
cleanupList ??= new();

cleanupList.Add(de.Key);
}
}

if (cleanupList is not null)
{
foreach (object o in cleanupList)
foreach (HashKey o in cleanupList)
{
_listManagers.Remove(o);
}
Expand Down

0 comments on commit 93a34d1

Please sign in to comment.