@@ -11,6 +11,16 @@ namespace GenericCollections
1111 [ TestClass ]
1212 public class ListTests
1313 {
14+ [ Cleanup ]
15+ public void CleanupTests ( )
16+ {
17+ // StaticListHolder is shared static state (see nanoframework/Home#1821
18+ // repro below); clear it so List_StaticField_* tests start empty on
19+ // every run/retry, even if a prior test's assertions failed.
20+ StaticListHolder . Numbers . Clear ( ) ;
21+ StaticListHolder . Items . Clear ( ) ;
22+ }
23+
1424 [ TestMethod ]
1525 public void List_Constructor_Default ( )
1626 {
@@ -1203,6 +1213,32 @@ public void List_GenericMethod_NewObj_InsertAndIterate()
12031213 Assert . AreEqual ( 4 , result [ 3 ] ) ;
12041214 }
12051215
1216+ [ TestMethod ]
1217+ public void List_StaticField_ValueType_Add ( )
1218+ {
1219+ // Reproduces nanoframework/Home#1821: a List<T> held in a static readonly
1220+ // field initialized via a field initializer (i.e. constructed from the
1221+ // declaring type's own .cctor), then Add()'d to from another method.
1222+ StaticListHolder . Numbers . Add ( 1 ) ;
1223+ StaticListHolder . Numbers . Add ( 2 ) ;
1224+
1225+ Assert . AreEqual ( 2 , StaticListHolder . Numbers . Count ) ;
1226+ Assert . AreEqual ( 1 , StaticListHolder . Numbers [ 0 ] ) ;
1227+ Assert . AreEqual ( 2 , StaticListHolder . Numbers [ 1 ] ) ;
1228+ }
1229+
1230+ [ TestMethod ]
1231+ public void List_StaticField_ReferenceType_Add ( )
1232+ {
1233+ // Reproduces nanoframework/Home#1821 with a reference-type element,
1234+ // mirroring the reported List<Ds18b20> Sensors static field.
1235+ StaticListHolder . Items . Add ( new DummyClass ( 1 , "One" ) ) ;
1236+
1237+ Assert . AreEqual ( 1 , StaticListHolder . Items . Count ) ;
1238+ Assert . AreEqual ( 1 , StaticListHolder . Items [ 0 ] . Id ) ;
1239+ Assert . AreEqual ( "One" , StaticListHolder . Items [ 0 ] . Name ) ;
1240+ }
1241+
12061242 // Generic helper methods that exercise NEWOBJ with MVAR TypeSpecs.
12071243 // Each method creates a new List<T> from within a generic method body,
12081244 // producing IL with MethodRef tokens whose owner TypeSpec is List<!!0>.
@@ -1316,6 +1352,14 @@ public DummyClass(int id, string name)
13161352 }
13171353 }
13181354
1355+ // Field initializers below run as part of this type's own .cctor, matching the
1356+ // pattern reported in nanoframework/Home#1821 (`private static readonly List<T> ... = new List<T>();`).
1357+ internal static class StaticListHolder
1358+ {
1359+ internal static readonly List < int > Numbers = new List < int > ( ) ;
1360+ internal static readonly List < DummyClass > Items = new List < DummyClass > ( ) ;
1361+ }
1362+
13191363 internal struct DummyStruct
13201364 {
13211365 public int Id { get ; set ; }
0 commit comments