Skip to content

Commit c55f9c5

Browse files
committed
add support for nullability annotations
1 parent 5ca16b7 commit c55f9c5

File tree

1 file changed

+26
-6
lines changed
  • src/Smdn.Reflection.ReverseGenerating.ListApi.MSBuild.Tasks/Smdn.Reflection.ReverseGenerating.ListApi.MSBuild.Tasks

1 file changed

+26
-6
lines changed

src/Smdn.Reflection.ReverseGenerating.ListApi.MSBuild.Tasks/Smdn.Reflection.ReverseGenerating.ListApi.MSBuild.Tasks/GenerateApiList.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
using System.Collections.Generic;
55
using System.Diagnostics.CodeAnalysis;
66
using System.IO;
7+
#if SYSTEM_REFLECTION_NULLABILITYINFOCONTEXT
8+
using System.Reflection;
9+
#endif
710
using System.Text;
811

912
using Microsoft.Build.Framework;
@@ -103,7 +106,8 @@ private ApiListWriterOptions BuildApiListWriterOptions()
103106
if (Enum.TryParse<MethodBodyOption>(GenerateMethodBody, out var methodBody))
104107
options.MemberDeclaration.MethodBody = methodBody;
105108

106-
options.Writer.OrderStaticMembersFirst = GenerateStaticMembersFirst;
109+
options.Writer.WriteNullableAnnotationDirective = true;
110+
options.Writer.OrderStaticMembersFirst = GenerateStaticMembersFirst;
107111

108112
options.AttributeDeclaration.TypeFilter = AttributeFilter.Default;
109113

@@ -140,12 +144,28 @@ private static void WriteApiListFile(
140144
encoding: new UTF8Encoding(false) // TODO: make encoding configurable
141145
);
142146

143-
var writer = new ApiListWriter(outputWriter, assm, arg.options);
144-
145-
writer.WriteAssemblyInfoHeader();
146-
writer.WriteExportedTypes();
147+
try {
148+
#if SYSTEM_REFLECTION_NULLABILITYINFOCONTEXT
149+
// assign NullabilityInfoContext to each assembly
150+
var nullabilityInfoContext = new NullabilityInfoContext();
147151

148-
return arg.outputApiListFilePath;
152+
arg.options.TypeDeclaration.NullabilityInfoContext = nullabilityInfoContext;
153+
arg.options.MemberDeclaration.NullabilityInfoContext = nullabilityInfoContext;
154+
#endif
155+
var writer = new ApiListWriter(outputWriter, assm, arg.options);
156+
157+
writer.WriteAssemblyInfoHeader();
158+
writer.WriteExportedTypes();
159+
160+
return arg.outputApiListFilePath;
161+
}
162+
finally {
163+
#if SYSTEM_REFLECTION_NULLABILITYINFOCONTEXT
164+
// release the references held by the NullabilityInfoContext so that the assembly can be unloaded
165+
arg.options.TypeDeclaration.NullabilityInfoContext = null;
166+
arg.options.MemberDeclaration.NullabilityInfoContext = null;
167+
#endif
168+
}
149169
}
150170
);
151171

0 commit comments

Comments
 (0)