This project is meant to be a learning opportunity for me, to dive deeper into Roslyn analyzers and code generators. During my FHL week (21-25.03.2022) I will learn how analyzers and code generators work, when in the compilation pipeline they're invoked, how to write them and how to debug them.
The project has a goal - create a new package for Stride Stride.Core.Compilation
under netstandard2.0
which will supersede AssemblyProcessor in the following:
For each type with [DataContract]
generate a serializer and register it in the SerializerSelector
with a [DataSerializerGlobal(Type, Profile)]
.
Testing: validate correctness against AssemblyProcessor by using dotPeak to deserialize the result.
In a class marked with [DataContract]
or if its base class is marked with it with Inherited = true
,
for every public field, property with get&set, or one marked with [DataMember]
, check if its type is serializable
and if not emit a warning. If it's marked with [DataMemberIgnore]
do not emit warning if non-serializable.
Testing: create a few classes and see warnings
For each attribute/interface marked with [AssemblyScan]
find classes marked with that attribute/implementing that interface
and register them in a dictionary in the AssemblyRegistry
.
Testing: verify the same code is generated as by AssemblyProcessor.
Working with Roslyn symbols - https://www.meziantou.net/working-with-types-in-a-roslyn-analyzer.htm
- Overview: https://github.com/dotnet/roslyn/blob/main/docs/features/source-generators.md
- Samples: https://devblogs.microsoft.com/dotnet/new-c-source-generator-samples/
- JSON generator:
- Incremental generator (perf): https://andrewlock.net/exploring-dotnet-6-part-9-source-generator-updates-incremental-generators/
- Regex generator: dotnet/runtime#59186
My previous (and failed) attempt: https://github.com/manio143/StructuredEntityGenerator/blob/master/StructuredEntityGenerator/StructuredEntityGenerator.cs
For my needs I will be creating an implementation of ISourceGenerator
with [Generator]
attribute that performs work in the Execute
method which will be invoked at compile time and not from the IDE.
- This series: https://www.meziantou.net/writing-a-roslyn-analyzer.htm
- This tutorial: https://docs.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/tutorials/how-to-write-csharp-analyzer-code-fix
- Official analyzers: https://github.com/dotnet/roslyn-analyzers
Since my analyzer will be contextual to serialization and not involved in syntax - I shall simply emit warning from the code generator.
This will be useful to depricate the custom https://github.com/stride3d/stride/blob/master/sources/core/Stride.Core/ModuleInitializerAttribute.cs which has a special processor https://github.com/stride3d/stride/blob/master/sources/core/Stride.Core.AssemblyProcessor/ModuleInitializerProcessor.cs