- Source Code Generator
- Roslyn
- Application of Source Code Generator
- Reflection
- Attributes
- Implementation
- Reference
-
Usually, we write code to generate code of another format. For example, we write Java or JavaScript code and then transpile it into machine code.
-
.NET allows us to generate C# code from existing C# code, which is called a source code generator.
-
Source code generators utilize Roslyn for generating code.
-
As the name suggests, we can only add new code; we can't modify existing code because the syntax tree provided by Roslyn is immutable. That means when we modify it, it will create a new copy of that syntax tree.
-
A .NET compiler platform (Open box) that allows developers to see what is happening during compilation, Unlike a typical compiler, which functions as a closed box. We provide source code as input, and it produces a common intermediate language as output, with developers having no access to any compiler activities.
-
Roslyn, being an open box, enables developers to access different processes in the compilation pipeline by providing an API for each process. This capability aids in building source code generators, code refactoring, intellisense, etc.
-
Roslyn helps format code, which is plain text, into a data structure like a tree and allows access to it using syntax API.
-
The syntax tree is used by the source code to traverse the code and find locations where we need to generate code.
-
The syntax tree has three objects: Node (a part of the code like a class, method, etc.), Token (a building block of nodes), and Trivia (additional information omitted by compilers like comments, whitespace, etc.).
-
It is used to enhance JSON serialization by creating metadata for a class. This metadata is usually generated by reflection during runtime while mapping data to an object and gets cached, Therefore prior generation of metadata improves the initial startup of the application.
-
It helps in creating boilerplate and repetitive code, thus improving the performance and productivity of developers.
-
As JSON schema serves as the source of truth for people across different teams involved in data transfer, code can be generated for that JSON schema without the need for manual coding.
-
Reflection helps inspect and obtain information about types, classes, methods, and more
-
It aids in binding a value or invoking a method in an assembly, and it is mostly used in JSON serialization and deserialization.
-
Reflection also assists in creating types based on user input or JSON during runtime.
-
Attributes assist in querying types based on their attributes.
-
Attribute helps us to provide metadata to types, classes, methods, etc.,
-
We can create Attributes by extending the attribute class
-
The Roslyn NuGet package provides an interface named incremental source generator for code generation.
-
In a class library project, when used as a reference in another project, Ensure proper referencing when using it as a reference in a class library project.
-
Utilize the Roslyn API to analyze the code (syntax tree) during source code generation.
-
Inform the compiler about it by specifying outputItemType = "Analyzer" in the case of a project reference.
-
Place the built DLL in the folder Analyzer/dotnet/cs in the case of NuGet, ensuring its availability in the user's project upon download.
-
During compilation, the compiler is aware of the code analyzer used in the referenced library or NuGet package.
-
Compilation starts and the source code generator begins to analyze the syntax tree using the Roslyn API, generating code.
-
The compilation then resumes, incorporating the generated code into the final executable.
-
High-Performance JSON Serialization With Code Generation on C# 11 and .NET 7.0 by Ian Griffiths
-
Let's Code an incremental source generator with Roslyn - Stefan Pölz - NDC Oslo 2023
-
Writing Code with Code: Getting Started with the Roslyn APIs - Steve Gordon - NDC London 2023
-
Creating NuGet Packages the easy way with .NET Standard in C#
-
Attribute in C# / .NET [C# Fundamental series, examples in .NET 6 ]