Skip to content

Commit f3a237d

Browse files
committed
Update
1 parent 2a3d851 commit f3a237d

File tree

3 files changed

+47
-23
lines changed

3 files changed

+47
-23
lines changed

src/.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,6 +1479,9 @@ dotnet_diagnostic.S1186.severity = error
14791479
# S1206: "Equals(Object)" and "GetHashCode()" should be overridden in pairs
14801480
dotnet_diagnostic.S1206.severity = error
14811481

1482+
# "GC.Collect" should not be called
1483+
dotnet_diagnostic.S1215.severity = error
1484+
14821485
# Floating point numbers should not be tested for equality 浮点数不应测试相等性
14831486
dotnet_diagnostic.S1244.severity = error
14841487

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,70 @@
1+
using System.Diagnostics.CodeAnalysis;
12
using System.Runtime.CompilerServices;
23

34
namespace CollectionTest;
45

56
public 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+
}
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1+
namespace CollectionTest;
12

3+
internal class Program
4+
{
25

3-
using CollectionTest;
6+
#region Constants & Statics
47

5-
//SortedSetTests.Order_Test();
8+
private static void Main(string[] args)
9+
{
10+
//SortedSetTests.Order_Test();
611

7-
//SortedListTests.Order_Test();
12+
//SortedListTests.Order_Test();
813

9-
ConditionalWeakTableTests.Test();
14+
ConditionalWeakTableTests.Test();
1015

11-
Console.ReadLine();
16+
Console.ReadLine();
17+
}
18+
19+
#endregion
20+
21+
}

0 commit comments

Comments
 (0)