@@ -15,82 +15,82 @@ namespace System.Runtime;
1515/// </summary>
1616internal class DependentHandle : IDisposable
1717{
18- private readonly ConditionalWeakTable < object , object > _table ;
19- private readonly WeakReference < object > _primaryRef ;
20- private bool _disposed ;
18+ private readonly ConditionalWeakTable < object , object > _table ;
19+ private readonly WeakReference < object > _primaryRef ;
20+ private bool _disposed ;
2121
22- /// <summary>
23- /// Initializes a new instance of DependentHandle with a primary and dependent object.
24- /// </summary>
25- /// <param name="primary">The primary object that controls the lifetime of the dependent object.</param>
26- /// <param name="dependent">The dependent object that will be collected when primary is collected.</param>
27- public DependentHandle ( object primary , object ? dependent )
28- {
29- _table = new ConditionalWeakTable < object , object > ( ) ;
30- _primaryRef = new WeakReference < object > ( primary ) ;
22+ /// <summary>
23+ /// Initializes a new instance of DependentHandle with a primary and dependent object.
24+ /// </summary>
25+ /// <param name="primary">The primary object that controls the lifetime of the dependent object.</param>
26+ /// <param name="dependent">The dependent object that will be collected when primary is collected.</param>
27+ public DependentHandle ( object primary , object ? dependent )
28+ {
29+ _table = new ConditionalWeakTable < object , object > ( ) ;
30+ _primaryRef = new WeakReference < object > ( primary ) ;
3131
32- // Store the dependent object in the table, keyed by the primary object
33- if ( dependent is not null )
34- {
35- _table . Add ( primary , dependent ) ;
36- }
37- }
32+ // Store the dependent object in the table, keyed by the primary object
33+ if ( dependent is not null )
34+ {
35+ _table . Add ( primary , dependent ) ;
36+ }
37+ }
3838
39- /// <summary>
40- /// Gets the primary object if it's still alive, otherwise returns null.
41- /// </summary>
42- public object ? Target
43- {
44- get
45- {
46- if ( _disposed )
47- return null ;
39+ /// <summary>
40+ /// Gets the primary object if it's still alive, otherwise returns null.
41+ /// </summary>
42+ public object ? Target
43+ {
44+ get
45+ {
46+ if ( _disposed )
47+ return null ;
4848
49- return _primaryRef . TryGetTarget ( out var target ) ? target : null ;
50- }
51- }
49+ return _primaryRef . TryGetTarget ( out var target ) ? target : null ;
50+ }
51+ }
5252
53- /// <summary>
54- /// Gets the dependent object if the primary object is still alive, otherwise returns null.
55- /// </summary>
56- public object ? Dependent
57- {
58- get
59- {
60- if ( _disposed )
61- return null ;
53+ /// <summary>
54+ /// Gets the dependent object if the primary object is still alive, otherwise returns null.
55+ /// </summary>
56+ public object ? Dependent
57+ {
58+ get
59+ {
60+ if ( _disposed )
61+ return null ;
6262
63- if ( _primaryRef . TryGetTarget ( out var primary ) &&
64- _table . TryGetValue ( primary , out var dependent ) )
65- {
66- return dependent ;
67- }
63+ if ( _primaryRef . TryGetTarget ( out var primary ) &&
64+ _table . TryGetValue ( primary , out var dependent ) )
65+ {
66+ return dependent ;
67+ }
6868
69- return null ;
70- }
71- }
69+ return null ;
70+ }
71+ }
7272
73- /// <summary>
74- /// Checks if both primary and dependent objects are still alive.
75- /// </summary>
76- public bool IsAllocated => Target is not null && Dependent is not null ;
73+ /// <summary>
74+ /// Checks if both primary and dependent objects are still alive.
75+ /// </summary>
76+ public bool IsAllocated => Target is not null && Dependent is not null ;
7777
78- /// <summary>
79- /// Disposes the DependentHandleCWT, clearing all references.
80- /// </summary>
81- public void Dispose ( )
82- {
83- if ( _disposed )
84- return ;
78+ /// <summary>
79+ /// Disposes the DependentHandleCWT, clearing all references.
80+ /// </summary>
81+ public void Dispose ( )
82+ {
83+ if ( _disposed )
84+ return ;
8585
86- _disposed = true ;
86+ _disposed = true ;
8787
88- // Clear the table - this will allow dependent objects to be collected
89- // even if the primary object is still alive
90- if ( _primaryRef . TryGetTarget ( out var primary ) )
91- {
92- _table . Remove ( primary ) ;
93- }
94- }
88+ // Clear the table - this will allow dependent objects to be collected
89+ // even if the primary object is still alive
90+ if ( _primaryRef . TryGetTarget ( out var primary ) )
91+ {
92+ _table . Remove ( primary ) ;
93+ }
94+ }
9595}
9696#endif
0 commit comments