Writing the first generator
The source generators in C# have to live in their own project. This is because the generators are then referenced by the project as analyzers. After all, we don’t want to have generators as part of our DLL, only the generated code from them.
Note
If you create the NuGet package from your generator, you can use that package in the same way as other NuGet packages.
Let’s start by creating a new project in our MediaLibrary solution:
- Open the MediaLibrary demo project and in the Solution Explorer area, right-click on the Solution ‘MediaLibrary’ item at the top of the window and choose Add | New Project….
- In the Add a new project window, find the Class Library project template in the Recent project templates area, or use the Search for templates input, and click Next.
- Set Project name to
MediaLibrary.Generators
and click Next. - In the Additional information window, select.NET Standard 2.0 in the...