From 500c6654bdd9970c06cc448185efe8702769e312 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 28 Feb 2023 10:45:33 +0000 Subject: [PATCH 1/2] add nullable fix --- InterfaceGenerator/AutoInterfaceGenerator.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/InterfaceGenerator/AutoInterfaceGenerator.cs b/InterfaceGenerator/AutoInterfaceGenerator.cs index e4ec264..c74ffc6 100644 --- a/InterfaceGenerator/AutoInterfaceGenerator.cs +++ b/InterfaceGenerator/AutoInterfaceGenerator.cs @@ -145,6 +145,8 @@ private string GenerateInterfaceCode(INamedTypeSymbol implTypeSymbol, AttributeD var interfaceName = InferInterfaceName(implTypeSymbol, attributeData); var visibilityModifier = InferVisibilityModifier(implTypeSymbol, attributeData); + //https://stackoverflow.com/questions/55492214/the-annotation-for-nullable-reference-types-should-only-be-used-in-code-within-a fix for nullable + codeWriter.WriteLine("#nullable enable"); codeWriter.WriteLine("namespace {0}", namespaceName); codeWriter.WriteLine("{"); @@ -163,6 +165,7 @@ private string GenerateInterfaceCode(INamedTypeSymbol implTypeSymbol, AttributeD --codeWriter.Indent; codeWriter.WriteLine("}"); + codeWriter.WriteLine("#nullable restore"); codeWriter.Flush(); stream.Seek(0, SeekOrigin.Begin); From 74337459d3a87b054a728c6cff43e50823f3acf1 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 28 Feb 2023 10:48:31 +0000 Subject: [PATCH 2/2] test annotation --- InterfaceGenerator/AutoInterfaceGenerator.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/InterfaceGenerator/AutoInterfaceGenerator.cs b/InterfaceGenerator/AutoInterfaceGenerator.cs index c74ffc6..ae04430 100644 --- a/InterfaceGenerator/AutoInterfaceGenerator.cs +++ b/InterfaceGenerator/AutoInterfaceGenerator.cs @@ -146,7 +146,10 @@ private string GenerateInterfaceCode(INamedTypeSymbol implTypeSymbol, AttributeD var visibilityModifier = InferVisibilityModifier(implTypeSymbol, attributeData); //https://stackoverflow.com/questions/55492214/the-annotation-for-nullable-reference-types-should-only-be-used-in-code-within-a fix for nullable - codeWriter.WriteLine("#nullable enable"); + if (implTypeSymbol.NullableAnnotation == NullableAnnotation.Annotated) + { + codeWriter.WriteLine("#nullable enable"); + } codeWriter.WriteLine("namespace {0}", namespaceName); codeWriter.WriteLine("{"); @@ -165,7 +168,10 @@ private string GenerateInterfaceCode(INamedTypeSymbol implTypeSymbol, AttributeD --codeWriter.Indent; codeWriter.WriteLine("}"); - codeWriter.WriteLine("#nullable restore"); + if (implTypeSymbol.NullableAnnotation == NullableAnnotation.Annotated) + { + codeWriter.WriteLine("#nullable restore"); + } codeWriter.Flush(); stream.Seek(0, SeekOrigin.Begin);