1+ using System . Diagnostics . CodeAnalysis ;
12using System . Runtime . CompilerServices ;
23
34namespace CollectionTest ;
45
56public static class ConditionalWeakTableTests
67{
8+
9+ #region Constants & Statics
10+
11+ [ SuppressMessage ( "Critical Code Smell" , "S1215:\" GC.Collect\" should not be called" , Justification = "<Pending>" ) ]
712 public static void Test ( )
813 {
14+ //需要 release 下,start without debug
15+
916 var mc1 = new ManagedClass { I = 1 } ;
1017 var mc2 = new ManagedClass { I = 2 } ;
1118 var mc3 = new ManagedClass { I = 3 } ;
1219 var mc4 = new ManagedClass { I = 4 } ;
1320
1421 var cwt = new ConditionalWeakTable < ManagedClass , ClassData >
15- {
16- { mc1 , new ClassData ( ) } , { mc2 , new ClassData ( ) } , { mc3 , new ClassData ( ) } , { mc4 , new ClassData ( ) }
17- } ;
22+ { { mc1 , new ClassData ( ) } , { mc2 , new ClassData ( ) } , { mc3 , new ClassData ( ) } , { mc4 , new ClassData ( ) } } ;
1823
1924 var wr2 = new WeakReference ( mc2 ) ;
2025 mc2 = null ;
2126
2227 foreach ( var ele in cwt )
2328 {
24- Console . WriteLine ( "{0 } Data created at {1}" , ele . Key . I , ele . Value . CreationTime ) ;
29+ Console . WriteLine ( $ " { ele . Key . I } Data created at { ele . Value . CreationTime } " ) ;
2530 }
2631
2732 GC . Collect ( ) ;
2833
2934 Thread . Sleep ( 100 ) ;
3035
3136 var count = 10 ;
32- while ( count -- > 0 ) //release 下运行
37+ while ( count -- > 0 )
3338 {
3439 if ( wr2 . Target == null )
3540 {
3641 Console . WriteLine ( "No strong reference to mc2 exists." ) ;
3742 }
3843 else if ( cwt . TryGetValue ( ( ManagedClass ) wr2 . Target , out var data ) )
3944 {
40- Console . WriteLine ( "mc2 Data created at {0}" , data . CreationTime ) ;
45+ Console . WriteLine ( $ "mc2 Data created at { data . CreationTime } " ) ;
4146 }
4247 else
4348 {
4449 Console . WriteLine ( "mc2 not found in the table." ) ;
4550 }
4651
47- if ( cwt . TryGetValue ( mc1 , out var data2 ) )
48- {
49- Console . WriteLine ( "mc1 Data created at {0}" , data2 . CreationTime ) ;
50- }
52+ // if (cwt.TryGetValue(mc1, out var data2))
53+ // {
54+ // Console.WriteLine($ "mc1 Data created at {data2.CreationTime}" );
55+ // }
5156
5257 foreach ( var ele in cwt )
5358 {
54- Console . WriteLine ( "{0 } Data created at {1}" , ele . Key . I , ele . Value . CreationTime ) ;
59+ Console . WriteLine ( $ " { ele . Key . I } Data created at { ele . Value . CreationTime } " ) ;
5560 }
5661
5762 Console . WriteLine ( "once" ) ;
5863 GC . Collect ( ) ;
5964 Thread . Sleep ( 100 ) ;
6065
61- //Release 输出:为什么只有 mc1 mc4 不被释放,
66+ //? 为什么只有 mc1 mc4 不被释放,
67+ //Release 输出:
6268 //1 Data created at 2022-09-13 17:38:19
6369 //2 Data created at 2022-09-13 17:38:19
6470 //3 Data created at 2022-09-13 17:38:19
@@ -71,23 +77,28 @@ public static void Test()
7177 }
7278 }
7379
80+ #endregion
81+
7482 private class ManagedClass
7583 {
84+
85+ #region Properties
86+
7687 public int I { get ; set ; }
88+
89+ #endregion
7790 }
7891
7992 private class ClassData
8093 {
81- public DateTime CreationTime ;
82-
83- public object Data ;
84-
8594 public ClassData ( )
8695 {
8796 CreationTime = DateTime . Now ;
8897 Data = new object ( ) ;
8998 }
90- }
91- }
9299
100+ public DateTime CreationTime ;
93101
102+ public object Data ;
103+ }
104+ }
0 commit comments