@@ -172,22 +172,38 @@ private void CommandMain(ParseResult parseResult, IConsole console)
172172 context : out var context ,
173173 loadIntoReflectionOnlyContext : loadAssemblyIntoReflectionOnlyContext ,
174174 actionWithLoadedAssembly : static ( assm , arg ) => {
175- var outputFilePath = GetOutputFilePathOf ( assm , arg . outputDirectory ) ;
175+ try {
176+ #if SYSTEM_REFLECTION_NULLABILITYINFOCONTEXT
177+ // assign NullabilityInfoContext to each assembly
178+ var nullabilityInfoContext = new NullabilityInfoContext ( ) ;
176179
177- // ensure the output directory existing
178- arg . outputDirectory . Create ( ) ;
180+ arg . options . TypeDeclaration . NullabilityInfoContext = nullabilityInfoContext ;
181+ arg . options . MemberDeclaration . NullabilityInfoContext = nullabilityInfoContext ;
182+ #endif
183+ var outputFilePath = GetOutputFilePathOf ( assm , arg . outputDirectory ) ;
184+
185+ // ensure the output directory existing
186+ arg . outputDirectory . Create ( ) ;
179187
180- using var outputWriter = new StreamWriter ( outputFilePath , append : false , new UTF8Encoding ( false ) ) ;
188+ using var outputWriter = new StreamWriter ( outputFilePath , append : false , new UTF8Encoding ( false ) ) ;
181189
182- var writer = new ApiListWriter ( outputWriter , assm , arg . options ) ;
190+ var writer = new ApiListWriter ( outputWriter , assm , arg . options ) ;
183191
184- writer . WriteAssemblyInfoHeader ( ) ;
185- writer . WriteExportedTypes ( ) ;
192+ writer . WriteAssemblyInfoHeader ( ) ;
193+ writer . WriteExportedTypes ( ) ;
186194
187- arg . logger ? . LogDebug ( $ "generated API list { outputFilePath } ") ;
188- arg . logger ? . LogInformation ( $ "{ assm . Location } -> { outputFilePath } ") ;
195+ arg . logger ? . LogDebug ( $ "generated API list { outputFilePath } ") ;
196+ arg . logger ? . LogInformation ( $ "{ assm . Location } -> { outputFilePath } ") ;
189197
190- return outputFilePath ;
198+ return outputFilePath ;
199+ }
200+ finally {
201+ #if SYSTEM_REFLECTION_NULLABILITYINFOCONTEXT
202+ // release the references held by the NullabilityInfoContext so that the assembly can be unloaded
203+ arg . options . TypeDeclaration . NullabilityInfoContext = null ;
204+ arg . options . MemberDeclaration . NullabilityInfoContext = null ;
205+ #endif
206+ }
191207 }
192208 ) ;
193209
0 commit comments