177 questions with no answers
0
votes
0
answers
75
views
Source code generator fails to match attribute declared on class against the actual attribute definition
Use Case
Given a class with a specific attribute, during compilation, look for classes with that attribute, evaluate all the properties for the class, add [Display(Name = "some value")] to ...
0
votes
0
answers
68
views
Using Compilation at later stages in generation pipeline
My generator needs to find all types in referenced projects which meet a specific criteria and generate a class for each of them using their method return types.
Example input (in a referenced project,...
2
votes
0
answers
111
views
Roslynator fail with C# source generator
I have a project specific roslyn source generator. It is a project reference in a library (which is also a project reference) in my application.
<ProjectReference Include="$(RepoRoot)\...
1
vote
0
answers
90
views
Roslyn Analyzer: Check values of readonly fields
I have a bit of a peculiar problem at work: In this project all of the ErrorCodes are defined in the code base. They look somewhat like this:
public readonly ErrorMessageCode 00001 = new ...
0
votes
0
answers
137
views
How to get the Symbollnfo (or TypeSymbol) of ObjectCreationExpressionSyntax via MemberAccessExpressionSyntax
I use Roslyn to analyze field initializations. One of the analysis target is the following field.
private static readonly ImmutableDictionary<int, int> _Map =
new Dictionary<int, int> { ...
0
votes
0
answers
81
views
Better way to search for generic Task<T> expression
I am on a roslyn analyzer that can handle async Task<T> boxing to an object variable like:
int GetValue() => ...
void Print(object value) => Console.WriteLine(value);
void Main() {
...
0
votes
0
answers
108
views
How can I verify types passed to method at design or compile time?
I'm in a situation where there are many generated types from different domains/projects/systems, but there's a lot of overlap between them.
In the past I've used things like Automapper to provide some ...
0
votes
0
answers
30
views
I have a project where I allow users to run 'macros' against the data. This used to work fine, but I am now getting StackOverflowException errors
My project is a complex data modelling tool. To save me having to write bespoke reports for it, I added the facility for users to write macros to extract the data they need (in reality it is just me ...
0
votes
0
answers
205
views
Roslyn diagnostics reported through CompilationAnalysisContext.ReportDiagnostic do not show up in Visual Studio
I have created a Roslyn analyzer using the Analyzer with Code Fix template in Visual Studio 2022. Everything works in unit tests, but no diagnostics are shown up in Visual Studio.
A minimal example ...
4
votes
0
answers
302
views
In c# code cleanup, How to stop visual studio 2022 adding comments /* Unmerged change from project 'xxxx (net6.0)'
Environment:
Visual studio 2022, v17.8.3 with net8 support
I did cleanup to c# solution with 6 projects using profile2 .
I found that vs 2022 during code cleanup using profile2 add these like ...
1
vote
0
answers
94
views
VS 2022 Analyzer Project Help Needed
I'm interested in building a custom code analyzer for my organization. Following the steps outlined in this article, I created an analyzer solution in Visual Studio 2022. Without altering the code ...
0
votes
0
answers
113
views
How to use the OpenAIClient in a Roslyn Analyzer without DiagnosticScopeFactory blocking it?
Im trying to write a Roslyn Analyzer which searches in code comments for some kind of hashtags and generates a Gpt prompt from it using the codefix lightbulb.
However when sending the gpt request I ...
1
vote
0
answers
57
views
How to take some metadata to stakeholders of a different project, use Roslyn to inspect a solution, and generate this Output
The end goal is for me to be able to provide some metadata to stakeholders of a different project. In that other project, we have various permission-related interfaces, and I'd like to be able to ...
0
votes
0
answers
233
views
Is it possible to manually call a Roslyn Analyzer from a Nuget package in code to analyze external code?
Can you import a Roslyn Analyzer from a NuGet package and manually call/ use them in your own code for analysing an external solution (for example letting it analyze a public git repo that you ...
1
vote
0
answers
228
views
CSharpCompilation emit fails when referencing a .dll not part of the project
Background:
I have a c# file 'code.txt' which needs to be compiled and executed.
This file requires the CsvHelper package from nuget in order to be.
I have two projects which are identical with the ...
12
votes
0
answers
1k
views
Running DotNet Build Causes Microsoft.Build.Tasks.CodeAnalysis.dll Assembly Conflict
I'm having an issue trying to build a dotnet core project from command line that has these references:
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="2.6.1" />
<...
2
votes
0
answers
179
views
C# Source Generators create codefix suggested edit
I have a source generator which creates a warning, I know how this warning would be fixed and could apply this fix automatically if given the opportunity.
https://github.com/dotnet/roslyn/blob/main/...
0
votes
0
answers
187
views
Make block statements a compile error in C# via Rider or Visual Studio
We found the following bug in our code as a result of a git merge.
public static void error(ref string test)
{
var diff = 123 - new Random().Next();
if (diff == 0)
test = $"diff ==...
3
votes
0
answers
593
views
How to create a console application using Roslyn programmatically?
My goal is to create a console application using Roslyn programmatically:
string codeToCompile = @"
using System;
public class Program
{
public static void Main()
{
Console....
1
vote
0
answers
599
views
How to use Roslyn to determine code's line and column position in a source file
I am using Roslyn within a source generator to find certain c# method calls in a syntax tree. When found, I want to note the line and column number.
This answer works for the line number but not the ...
0
votes
0
answers
483
views
Roslyn analyser - check for null references for a specific type
I am checking for the possibility to build custom roslyn analyzer for case specifics to our system.
The solution is in .net Framework 4.8. I started with the tutorial How to write csharp analyzer code ...
0
votes
0
answers
84
views
Cannot access included files SourceGenerator
I added this ItemGroup to the SourceGenerator.csproj file
<ItemGroup>
<Content Include="defs\*\*.json" CopyToOutputDirectory="PreserveNewest" />
<...
2
votes
0
answers
508
views
Use dotnet-format to change to file-scoped namespaces in C#10
I want to format a file to "file-scoped namespaces". I want to make that change only.
My .editorconfig contains this only:
[*.cs]
csharp_style_namespace_declarations = file_scoped:warning ...
1
vote
0
answers
1k
views
Write Code Analyzer with Visual Studio 2022
I'm experimenting with the .NET Compiler Platform SDK. Currently I want to create an analyzer with code fix as described in this tutorial in the Microsoft documentation:
https://learn.microsoft.com/en-...
5
votes
0
answers
398
views
Roslyn analyzer to detect all kinds of implicit conversions?
I need a code analyzer which warns me about every implicit cast/conversion like when assigning a variable or passing an argument to method.
I started a tutorial series on Roslyn but it's still huge ...