Questions tagged [csharp-source-generator]
63 questions
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
11
votes
1 answer
In-memory CSharpCompilation cannot resolve attributes
I am trying to run C# source generators in-memory using the following code snippet:
var syntaxTree = await SyntaxTreeFromRelativeFile("testdata/IMyInterface.cs");
var compilation = CSharpCompilation.Create("compilation",…

Tom
- 532
- 5
- 19
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
1 answer
Can Source Generator created files be physically reached?
Is there any standard method/option, how to arrange the files generated via Source Generators and added to the build process are also physically visible in the project structure (so developer can review the result code e.g. via VS solution explorer…

sharpener
- 1,383
- 11
- 22
6
votes
1 answer
How to implement System.Text.Json source generator with a generic class?
I have a generic class like this:
public class Property
{
}
I would like to use the new System.Text.Json source generator but it does not seem to work for a generic class. Here is the derived JsonSerializerContext for that…

Kzryzstof
- 7,688
- 10
- 61
- 108
5
votes
1 answer
How to ship a .Net library with an included source generator?
I can't figure out how to ship a .Net library that also includes a Roslyn source generator, and have the source generator access the source code of the main project, without adding an explicit reference from the main project to the…

sw1337
- 533
- 4
- 16
5
votes
1 answer
When using the C#9 Source Generator, how to ensure files are generated to specific location/file path?
I'm working on a project utilizing C# 9's Source Generator, but when the code is generated, I'm wanting different files of generated code to be emitted to specific file paths/locations within the existing project where the code is being…

S. Dunlap
- 51
- 2
4
votes
1 answer
C# source generators: Is there a way to find type information of referenced projects?
Using C# source generators, is there a way to get more information about types in referenced assemblies. To be more precise: Is there a way to find out which type implements an interface that resides in a referenced project?
For example:
Assembly…

CodeChops
- 173
- 9
4
votes
2 answers
How to fix source-code generator with .NET 6?
I ported my .NET 5.0 application to .NET 6.0 and now I found that the source-code generator fails. It shows the following error:
Generator 'PermissionGenerator' failed to generate source. It will not contribute to the output and compilation errors…

Ramon de Klein
- 5,172
- 2
- 41
- 64
4
votes
2 answers
Roslyn analyzer: Is class marked as partial
Is there a way to find out whether class is partial inside Roslyn analyzer?
There is a PartialImplementationPart in IMethodSymbol, but nothing similar for INamedTypeSymbol.
I'm writing a Source Generator, and I want to generate second part of the…

maxc137
- 2,291
- 3
- 20
- 32
3
votes
1 answer
VS and Rider ignore source generator generated files
C# Source generated files are ignored or show errors but when I copy the file out everything works.
state.Text.g.cs
using System.Collections.Generic;
using System.Linq;
#nullable enable
namespace…

Drachencheat
- 41
- 4
3
votes
0 answers
C# Source Generator diagnostic documentation page link
I've written a Source generator which creates an error, I would love to be able to give the user more information about the specific error that they have by going to a documentation page.
Luckily DiagnosticDescriptor has a helpLinkUri field which is…

user3797758
- 829
- 1
- 14
- 28
3
votes
1 answer
.NET 6 source generators and C# 9 syntax
If source generator projects must target netstandard2.0 (C# 7.3), how and why do the examples in the .NET 6 incremental generator documentation use C# 9 syntax like static anonymous functions?
From the doc, formatted for…

Kevin Krumwiede
- 9,868
- 4
- 34
- 82
3
votes
0 answers
Incremental source generator not called in production build context
Given a working source generator and a working test project for the generator.
Generator
netstandard2.0
false
…

Prophet Lamb
- 530
- 3
- 17
3
votes
1 answer
Is there a way to configure a Source Generator from the consuming project?
I can configure a source generator by looking for a JSON file and using that, but this method is limited and generally slow to implement.
What I’m looking for is something like fluent syntax on the consuming project, e.g.
generator.UseFormat(c)
As…

Luke Parker
- 299
- 1
- 10