@@ -271,11 +271,6 @@ public void Cleanup()
271271 {
272272 type2s . Add ( enumerator . Key ) ;
273273 }
274-
275- #if ! NETSTANDARD2_1
276- // Do the cleanup for the list of keys that are weakly tracked too
277- enumerator . Value . Cleanup ( ) ;
278- #endif
279274 }
280275
281276 // Remove all the mappings with no handlers left
@@ -363,29 +358,20 @@ public bool Remove(TKey key)
363358
364359 /// <inheritdoc cref="IEnumerable{T}.GetEnumerator"/>
365360 public IEnumerator < KeyValuePair < TKey , TValue > > GetEnumerator ( )
366- {
367- foreach ( WeakReference < TKey > node in this . keys )
368- {
369- if ( node . TryGetTarget ( out TKey ? target ) &&
370- this . table . TryGetValue ( target , out TValue value ) )
371- {
372- yield return new KeyValuePair < TKey , TValue > ( target , value ) ;
373- }
374- }
375- }
376-
377- /// <summary>
378- /// Trims the list of tracked <typeparamref name="TKey"/> instances.
379- /// </summary>
380- public void Cleanup ( )
381361 {
382362 for ( LinkedListNode < WeakReference < TKey > > ? node = this . keys . First ; ! ( node is null ) ; )
383363 {
384364 LinkedListNode < WeakReference < TKey > > ? next = node . Next ;
385365
386- // Remove all the nodes with a target that has been collected
387- if ( ! node . Value . TryGetTarget ( out _ ) )
366+ // Get the key and value for the current node
367+ if ( node . Value . TryGetTarget ( out TKey ? target ) &&
368+ this . table . TryGetValue ( target ! , out TValue value ) )
369+ {
370+ yield return new KeyValuePair < TKey , TValue > ( target , value ) ;
371+ }
372+ else
388373 {
374+ // If the current key has been collected, trim the list
389375 this . keys . Remove ( node ) ;
390376 }
391377
0 commit comments