Beginner to Roslyn Source Generators here.
I'm following the documentation on Source Generators by Microsoft. I have created a .NET Standard 2.0 Class Library, and set the following as the contents of the csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<!-- Property below is required for [GeneratorAttribute] -->
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.11.0" />
</ItemGroup>
</Project>
Now, based on the sample code in the documentation, I created a new class with [Generator] attribute, that follows the ISourceGenerator interface:
[Generator]
public class Sample : ISourceGenerator
{
public void Execute(GeneratorExecutionContext context)
{
}
public void Initialize(GeneratorInitializationContext context)
{
}
}
But, when I add context.AddSource method invocation in the Execute method:
[Generator]
public class Sample : ISourceGenerator
{
public void Execute(GeneratorExecutionContext context)
{
context.AddSource("Foo.g.cs", "public class Bar { }");
}
public void Initialize(GeneratorInitializationContext context)
{
}
}
then I see this syntax error on context.AddSource method invocation:
RS1035 The symbol 'GeneratorExecutionContext' is banned for use by analyzers: Non-incremental source generators should not be used, implement IIncrementalGenerator instead
How can I solve this issue?