| GeneratorConsole | ||
| GeneratorLib | ||
| .editorconfig | ||
| .gitignore | ||
| license.txt | ||
| readme.md | ||
Source Generator Demo
This is an attempt at a bare-bones minimal source generator in an attemp to evolve to a project template or other boilerplate approach to creating source generators.
GeneratorConsole and HelloWorldGenerator
The HelloWorldGenerator should produce a static class with a single static method that can easily by called by any project that properly references the analyzer project, GeneratorLib. It should be called like this:
HelloWorldNamespace.HelloWorld.SayHello();
How to Greate a Source Generator
- Create a new class library for the generators.
- Change the class library target framework to "netstandard2.0", even if there is a newer version. This is a "plugin" for the analyzer system and must match its version.
- Manually add appropriate attributes to the .csproj file as listed below.
- Add appropriate NuGet references listed below.
Appropriate attributes for the generator .csproj project file:
<langVersion>12</langVersion>OR<langVersion>latest</langVersion><EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules><IsRoslynComponent>true</IsRoslynComponent><IncludeBuildOutput>false</IncludeBuildOutput><EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
Probably not needed attributes:
<CompilerGeneratedFilesOutputPath>.\GeneratedFiles</CompilerGeneratedFilesOutputPath>
Required NuGet references for generators:
Microsoft.CodeAnalysis.AnalyzersMicrosoft.CodeAnalysis.CSharp
Gen-1 analyzers also needed (Gen-2 probably does not and sometimes gives you a warning to remove it):
Microsoft.CodeAnalysis.CSharp.Workspaces
How to Consume a Source Generator
Create a new application of any kind or a new shared class library. Add a reference to the source generator library. This can be a NuGet reference. While under development, it will probably be a project reference. If it is a project reference, you will need to manually modify the reference in the consumer project's .csproj file manually to tell it that the assembly is an analyzer. It will need the OutputItemType attribute at least. Example:
<ItemGroup>
<ProjectReference Include="..\GeneratorLib\GeneratorLib.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false"/>
</ItemGroup>
Issues
-
When you get the warning "2>CSC : warning CS9057: The analyzer assembly 'mygeneratorproject.dll' references version '4.13.0.0' of the compiler, which is newer than the currently running version '4.12.0.0'.", it will NOT generate the sources! You must adjust the version of the Microsoft.Analyzers.CSharp Nuget package!
-
The
CompilerGeneratedFilesOutputPathgenerator project setting causes the build process to create the specified output folder (not under bin/ or obj/, however). But the sources never show up in there. There is also a "generated" folder under the obj/ folder, but nothing shows up there either. I don't know how to control the generated source output. If I debug and step into generated code, then do a SAVE-AS, it shows me that it's in theC:\Users\Phil.Gilmore\AppData\Local\Temp folder. -
References to the generated code DO work and the program DOES build and the program DOES run and the running program DOES work. However, the IDE editor and the ERRORS toolwindow show errors in the code that refernces generated types. As if it didn't build. VERY frustrating!