Skip to content

Instantly share code, notes, and snippets.

@manio143
Last active March 24, 2022 22:12
Show Gist options
  • Save manio143/ca161cc51a2b929858fbc04150091385 to your computer and use it in GitHub Desktop.
Save manio143/ca161cc51a2b929858fbc04150091385 to your computer and use it in GitHub Desktop.
Plans for Stride Roslyn extension for Spring FHL

Creating a Roslyn extension for Stride.Core

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.

Goals

The project has a goal - create a new package for Stride Stride.Core.Compilation under netstandard2.0 which will supersede AssemblyProcessor in the following:

DataSerializer generation

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.

DataContract analyzer

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

AssemblyScan generation

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.

Resources

Working with Roslyn symbols - https://www.meziantou.net/working-with-types-in-a-roslyn-analyzer.htm

Generators

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.

Analyzers

Since my analyzer will be contextual to serialization and not involved in syntax - I shall simply emit warning from the code generator.

Module initializer

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

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/module-initializers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment