Questions tagged [roslyn-code-analysis]

For questions regarding code analysis for C# and VB based on the Roslyn .NET compiler platform API.

The Roslyn compiler platform provides an API to implement code analysis, code fixes, and refactoring tools for C# and VB.

The Roslyn Wiki also contains an introduction for "How To Write a C# Analyzer and Code Fix" and for Visual Basic.

769 questions
0
votes
1 answer

Roslyn SonarAnalyzer.CSharp does not respect rule settings from SonarLint.xml

I am trying to customize some rules, particularly S134 but it does not take my deviation from the default max parameter into account Repro: Create new Console App in Visual Studio, Add SonarAnalyzer.CSharp NuGet package Add file SonarLint.xml, set…
metacircle
  • 2,438
  • 4
  • 25
  • 39
0
votes
1 answer

Canot resolve argument parameters in Roslyn Source Generation

I am having trouble getting parameter values from attributes in a source generator. I have an IIncrementalSourceGenerator that is supposed to look up a class type and inspect the attributes of that class. There is a single string parameter on the…
0
votes
0 answers

CA0052 Could not identify plateform And CA0055 not targets were selected when Enable code analysis VS 2019

I'm using vs 2019 with .Net 5 and tried to run code analysis with MsBuild command, When I was using FxCorp its worked perfectly but fxcorp is deprecated I was getting below error Warning CA0507 Post-build Code Analysis (FxCopCmd.exe) has been…
0
votes
1 answer

Roslyn Analyzers do not run often enough

I am writing my first Roslyn analyzers. I have basically followed the tutorial https://learn.microsoft.com/en-us/archive/msdn-magazine/2014/special-issue/csharp-and-visual-basic-use-roslyn-to-write-a-live-code-analyzer-for-your-api , and then…
ZbynekZ
  • 1,532
  • 10
  • 16
0
votes
1 answer

Specific Requirements for custom Roslyn Code Analyzer to run in live analysis?

I've got a Roslyn based Code Analyzer and Codefix. When directly creating the ReportDiagnostic from an AnalyzerCodeBlock, they would show up in live analysis (Problems in Jetbrains Rider). However, it needs to parse additional data from the solution…
0
votes
1 answer

Can we modify the existing assembly(dll) by using CSharpCompilation.Create()? If not how to do?

I am using CSharpCompilation.Create to create assembly from scratch. The thing I got to know is this always creates a new assembly. Is there anyway to add/modify the existing DLL in Roslyn? This is because I am building a tool where a user can type…
coding_cs
  • 77
  • 9
0
votes
0 answers

Set a parent for nodes produced by SyntaxFactory

I am using CSharpSyntaxRewriter to traverse the syntax tree and SyntaxFactory to replace some of the nodes, for the purpose of my own C#-like DSL. When I create a new node, e.g. a MemberAccessExpression, I would like to assign its Parent to…
Isolin
  • 845
  • 7
  • 20
0
votes
1 answer

generate getter and setter lambda with Roslyn

I am new to using Roslyn and the truth is that I am finding it quite interesting, but when using lambda expressions I am blocked. I would like to generate a property with getters and setters with lambda, so it looks like this: private string…
Numared
  • 1
  • 1
0
votes
1 answer

OpenSolutionAsync - Field not found: 'Microsoft.Build.Utilities.ChangeWaves.Wave16_8' on .NET Framework 4.8 / 4.7.2

This code works fine on .NET 6, but when I want to run it on .NET Framework 4.8, then that's what I receive System.MissingFieldException: Field not found: 'Microsoft.Build.Utilities.ChangeWaves.Wave16_8'. at…
Joelty
  • 1,751
  • 5
  • 22
  • 64
0
votes
1 answer

Ho to add project to all solution projects except itself?

I have analyzers project in my solution. I also use Directory.Build.props file. I'd like to reference analyzer project by all projects (except analyzer and its tests). What condition can I use for it? It is possible to get referencing project?
Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137
0
votes
0 answers

Roslyn analyzer - resolving base classes deep does not work during editing

We have some custom roslyn analyzers for one of our .net project. Some of the rules need to analyze all base classes deep of a given ClassDeclaration. The problem is, that we also need the SematicModel which belongs to the base classes in order to…
Lori
  • 155
  • 8
0
votes
1 answer

In Roslyn how to figure out that two symbols are equivalent?

Suppose I created two Compilation objects, initialized both with the same SyntaxTree, obtained two SemanticModel objects and used each to obtain a symbol to the same declaration. Here is the complete unit test. using Microsoft.CodeAnalysis; using…
mark
  • 59,016
  • 79
  • 296
  • 580
0
votes
1 answer

How to get the IMethodSymbol object given the matching IMethodSymbol object obtained through a different `SemanticModel` instance?

I obtained a IMethodSymbol object from the given SemanticModel, but then realized the symbol lacks the reference to its declaration syntax. Let us assume I can easily create the respect SyntaxTree object for any symbol. How can I "refresh" the…
mark
  • 59,016
  • 79
  • 296
  • 580
0
votes
1 answer

In a Roslyn Analyzer/Source Generator, is there any way to distinguish between direct and transitive assembly references

AIUI, transitive references are an MSBuild rather than a Roslyn feature, and are to do with Package/ProjectReferences rather than assembly references. I believe the compiler just gets a set of assembly references supplied to it and no information…
Tom
  • 1,204
  • 2
  • 10
  • 25
0
votes
0 answers

How to find .ToString() calls on enum values in C#

I am trying to use Roslyn analyzers to find all instances of using .ToString() on an enum value so that I can suggest a codefix to use the nameof() expression instead. How can I go about doing this? I've trudged through it for the string…
Duu82
  • 157
  • 2
  • 11