This tag is specifically intended for C# source generators as leveraged by Rosyln. For code generation in general, use [code-generation].
Questions tagged [sourcegenerators]
170 questions
13
votes
1 answer
Disabling a specific C# 9 source generator
Is there any way to disable a specific C# 9 source generator? Or alternatively disable them all?
the package in question is https://github.com/Husqvik/GraphQlClientGenerator#c-9-source-generator which is mean to be able to be used as both a lib and…

Simon
- 33,714
- 21
- 133
- 202
12
votes
1 answer
generate source based on other assembly classes (c# source generator)
I want to generate a static class that should have a method depending on other classes in specific reference assembly.
a simplified example:
// Generator.csproj
[Generator]
public class MyGenerator : ISourceGenerator
{
public void…

AliReza Sabouri
- 4,355
- 2
- 25
- 37
10
votes
1 answer
How to access project referenced assemblies in source generator?
I have a Class Library project First.csproj with one file ICar.cs:
namespace First
{
public interface ICar
{
string Name { get; set; }
}
}
I have an empty Class Library project Second.csproj and Analyzer (source generator)…

user1576055
- 567
- 1
- 4
- 16
10
votes
1 answer
Make diagnostic errors reported by a C# source generator appear in the Visual Studio editor
I am attempting to write a C# source generator that throws a warning/error under certain conditions using GeneratorExecutionContext.ReportDiagnostic. My source generator is able to run and output errors successfully upon building a sample project…

Douglas Dwyer
- 562
- 7
- 14
8
votes
1 answer
Roslyn source generator is "ignored"?
I'm creating a simple Roslyn Source Generator. The generator seems to work well in debug, correctly generating the desired class. However, when referencing it from another project in the solution, a red icon is shown near it with the "Ignored"…

Impworks
- 2,647
- 3
- 25
- 53
8
votes
1 answer
How can I make an incremental source generator run in Visual Studio?
I've followed the documentation to create a basic incremental source generator that outputs a copy of classes marked with an attribute:
// Code found in my project
[Copy]
public class MyClass
{
public int Value { get; set; }
}
// Output from…

Collin Dauphinee
- 13,664
- 1
- 40
- 71
8
votes
4 answers
Source Generators dependencies not loaded in Visual Studio
I am working on source generator and I have problems with dependencies:
It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'FileNotFoundException' with message 'Could not load file or assembly…

Krzysztof
- 15,900
- 2
- 46
- 76
8
votes
1 answer
How can I make a Roslyn Analyzer project a transitive dependency?
I have a library that relies on a source generator to work correctly. In the MyLibrary.csproj, I reference the generator like so.

chyyran
- 2,446
- 2
- 21
- 35
7
votes
2 answers
C# Source Generator not including results from Project Reference
Edit3: At some point this just started working. No clue why. Maybe it was a VS bug that got fixed?
Edit2: Looking into the Analyzers node in solution explorer, I've discovered the source generator runs successfully when I first open the program, and…

Salvador
- 239
- 4
- 7
7
votes
0 answers
How to publish this C# Source Generator with dependencies on Nuget?
I've created a C# source generator, and would like to publish it to Nuget.
But I run into warnings/errors such as:
Some target frameworks declared in the dependencies group of the nuspec and the lib/ref folder do not have exact matches in the other…

Josh
- 2,958
- 3
- 16
- 27
6
votes
1 answer
How to debug Roslyn source generator in VS 2022?
Context
I was able to debug my source generator in VS 2019 by setting the project properties/debug Launch to Roslyn Component, then the target project combo to one of my consuming project:
Now this feature seems to be missing in VS 2022, I can not…

g.pickardou
- 32,346
- 36
- 123
- 268
6
votes
1 answer
Use System.Text.Json source generator with custom JsonConverter
How are custom JsonConverter<> classes used together with System.Text.Json source generator?
I would like to call JsonSerializer.Deserialize() that doesn't fallback to a reflection-based implementation and uses a custom MyTypeJsonParser :…

zigzag
- 579
- 5
- 17
6
votes
1 answer
Where does .NET 6's System.Text.Json source generation store its code?
I have tried to implement Source Generation-based JSON serilization, based on MS Docs: How to use source generation in System.Text.Json. My code is as follows:
using System;
using System.Text.Json;
var person = new Person(){FirstName = "John",…

Ombrelin
- 547
- 1
- 8
- 27
5
votes
2 answers
C# source generators: How to debug compiler errors in the generated code?
I am experimenting with C# source generators. I have spent about a day on it, and I find it a very frustrating and painful experience. IntelliSense is extremely unreliable. It occasionally works, but most often it does not, and I have not been able…

Claus Appel
- 1,015
- 10
- 28
5
votes
2 answers
SourceGenerator: Attribute ConstructorArguments is empty
I am writing a source generator but am struggling to get the value of an argument passed to the constructor of my attribute.
I inject the following into the compilation:
namespace Richiban.Cmdr
{
[AttributeUsage(AttributeTargets.Method,…

Richiban
- 5,569
- 3
- 30
- 42