This project is a Proof of Concept (PoC) for creating a .NET Source Generator. Source Generators are a powerful feature in the .NET ecosystem, allowing developers to generate additional source code at compile time to simplify and enhance development workflows. This PoC demonstrates basic functionality and serves as a foundation for more advanced implementations.
For a detailed introduction to C# Source Generators, check out this excellent blog article.
Follow these instructions to set up the project on your local machine for development and testing. This guide will also include notes on deploying the project in a live system.
- Blog: Mastering at Source Generators
- Microsoft Documentation on Source Generators
- Roslyn GitHub Repository
Before running the project, ensure you have the following tools installed:
- .NET SDK 6.0 or later
- Visual Studio 2022 (with the ".NET Desktop Development" workload installed)
- Basic knowledge of C# and Roslyn APIs (optional but recommended)
Follow these steps to set up the development environment:
-
Clone the Repository
git clone https://github.com/your-repo/poc-source-generator.git cd poc-source-generator
-
Open the Solution
Open the
PoCSourceGenerator.sln
file in Visual Studio 2022. -
Build the Project
Build the solution to restore NuGet packages and ensure all dependencies are installed.
dotnet build
-
Run the Demo Project
- The solution includes a demo project where the Source Generator is applied. Run the demo project to see the generated code in action.
- Open the
Generated
folder (or the Output window) in Visual Studio to inspect the generated files.
- Add a reference to the Source Generator project in your main application.
- Annotate your code with attributes defined in the Source Generator.
- Build your solution to trigger code generation.
- Review the generated code in the
obj
folder of your project.
Here is an example of using the Source Generator:
[AutoGenerate]
public partial class ExampleClass
{
public string Name { get; set; }
}
public partial class ExampleClass
{
public string GeneratedProperty => $"Hello, {Name}!";
}
- Generated files are located in the
obj
directory by default. - Use the diagnostic tools in Visual Studio to troubleshoot issues related to code generation.
Contributions are welcome! If you have suggestions, bug reports, or feature requests, please create an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.