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

'Diagnostic analyzer runner' is currently unavailable due to an internal error

I'm using the Visual Studio 2022 (17.4.4). This is a .NET 6 C# project. When running code analysis on my project, I immediately get the message "Code analysis completed for 'projectname'". No code analysis warnings or errors are displayed. When I…
LTR
  • 1,226
  • 2
  • 17
  • 39
0
votes
1 answer

Roslyn Code Analysis: Visual Editing of Rules possible?

If I understand correctly, there are two kinds of code analysis available for C# Visual Studio projects: The old legacy analyzers (not available for .NET Core and .NET Standard) Roslyn Code Analyzers The old legacy analyzers used .ruleset files to…
LTR
  • 1,226
  • 2
  • 17
  • 39
0
votes
0 answers

DiagnosticAnalyzer for type usage

I would like to write DiagnosticAnalyzer, which analyzes if a class (ie. BotAggregate) declared in a MyProject.Domain assembly has it's own DbSet declared in MyProjectDbContext class which is placed in MyProject.EFCore assembly. When I…
Tomasz
  • 2,051
  • 3
  • 31
  • 64
0
votes
1 answer

Custom diagnostic rule not being applied in C# using Roslyn API

I have implemented a custom diagnostic rule in C# using the Roslyn API, but it is not being applied to my code. I have verified that the rule is included in the list of enabled rules in my project's code analysis settings and that code analysis is…
Mojtaba Khooryani
  • 1,763
  • 1
  • 17
  • 36
0
votes
1 answer

Error using the MSBuild.CodeAnalysis apis to analyze a C# project?

I am attempting to build a tool (using C# and .NET 7.0, Visual Studio 2022) that will analyze another C# project. I added the MSBuild.Locator package and invoked it with MSBuildLocator.RegisterDefaults(). However, when I attempt to actually open…
OrbitalDan
  • 305
  • 1
  • 2
  • 7
0
votes
0 answers

In Entity Framework i want to use roslyn analyzer and find the places where lazy loading might occur on some huge tables. Any idea on how to achieve?

In Entity Framework i want to use roslyn analyzer and find the places where lazy loading might occur on some huge tables. Any idea on how to achieve? I am trying to achieve and give warning while running the solution if a code is written in such a…
0
votes
0 answers

NuGet package restore in Roslyn analyzer test

In my unit tests for my Roslyn analyzer, I need to use Microsoft.AspNetCore.Mvc in the test code string. I believe that means I need the long form of testing, so I did the following. var expected = VerifyCS.Diagnostic(MyAnalyzer.DiagnosticId) …
Gargoyle
  • 9,590
  • 16
  • 80
  • 145
0
votes
2 answers

How can I detect missing attributes on a method with Roslyn Code analyser

I am following this tutorial, https://learn.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/tutorials/how-to-write-csharp-analyzer-code-fix What I really want is to detect if a method in a ASP.Net Web API controller is missing my Custom attribute and…
Danish
  • 694
  • 1
  • 7
  • 15
0
votes
0 answers

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 == 0"; { return; } //This will…
0
votes
0 answers

Roslyn: Add Alias To Namespace And Refactor All Associated Code

We are in the process of making a tool that refactors a big pile of VB.NET code. During the refactoring process, we discovered that there are many conflicts between some new namespaces we added and some classes from imported assemblies. We want to…
0
votes
1 answer

How to fix error loading DiagnosticAnalyzerFieldsAnalyzer

Visual Studio 2022 was installed on clean Windows 11 using Web Development profile. Running existing ASP.NET 6 MVC project in it using F5 throws error in build window NuGet package restore failed. Please see Error List window for detailed warnings…
Andrus
  • 26,339
  • 60
  • 204
  • 378
0
votes
1 answer

InsertTokensAfter() How to Add New Token after a. Existing SyntaxToken c# Roslyn

I want to add a after DeserializeObject like JsonConvert.DeserializeObject(json) Below is how I'm trying to achieve it var desiToken = node.DescendantTokens().Where(x => x.Text == "DeserializeObject").FirstOrDefault(); List
S G T
  • 1
  • 1
0
votes
0 answers

Duplicate DLL using Roslyn dynamically-compiled

I'm working on a project that dynamically compiles and executes a C# code, and we use the Npgsql library, so we are loading the Npgsql.dll as shown here, ie, we use AssemblyLoadContext after compiling the dynamic code: Assembly assembly =…
Rosicley
  • 1
  • 1
0
votes
1 answer

Unity Default.ruleset Doesnt Work As Expected / Roslyn Analyzer

Roslyn analyzers and source generators Expected Actual Unity 2021.3.11f1 / Rider 2022.2.3 Analyzers: ErrorProne.NET.CoreAnalyzers Only logging Assets\Subfolder\RethrowError's error. When I remove the subfolders code then logs the Assets\Rethrow's …
0
votes
0 answers

CSharpScripting variables passed are not

I'm trying to execute a script dynamically, via CSharpScript, following the example here: example public class Feature { public class Params { public TypeA ParamA { get; set; } public TypeB ParamB { get; set; } } …
Yafim Simanovsky
  • 531
  • 7
  • 26