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
6
votes
2 answers

Cannot create a compilation in Roslyn from source code

For test purposes, I need to get a System.Reflection.Assembly from a string source which contains a source code. I am using Roslyn: SyntaxTree tree = CSharpSyntaxTree.ParseText(source); CSharpCompilation compilation =…
Andry
  • 16,172
  • 27
  • 138
  • 246
6
votes
1 answer

How can I feed a loaded Assembly to a Roslyn Workspace in C#

I am enhancing an existing process with runtime code generation. The code I am creating at runtime needs access to some of the same dlls that the process creating the code is already referencing. Problem is that the process runs within some 3rd…
Wizbit
  • 421
  • 1
  • 4
  • 13
6
votes
1 answer

Find UsingDirectiveSyntax that belongs to InvocationExpressionSyntax

In the code below, the "Console.WriteLine" call needs the "System" using directive to work. I already have a UsingDirectiveSyntax object for "using System" and an InvocationExpressionSyntax object for "Console.Writeline". But how can I know, using…
TWT
  • 2,511
  • 1
  • 23
  • 37
6
votes
7 answers

Find missing await in solution

I have lots of async methods in my server code, but I suspect that I have callers without await. Is there a simple way to scan the code for calls where await is missing? public async Task DomeSomethingAsync() { var result = await GetResult(); …
Frode
  • 3,325
  • 1
  • 22
  • 32
6
votes
1 answer

Add a parameter to a method with a Roslyn CodeFixProvider

I'm writing a Roslyn Code Analyzer that I want to identify if an async method does not take a CancellationToken and then suggest a code fix that adds it: //Before Code Fix: public async Task Example(){} //After Code Fix public async Task…
Philip Pittle
  • 11,821
  • 8
  • 59
  • 123
6
votes
1 answer

Roslyn: Retrieving Symbol in parent or ancestor SyntaxNode

I'm writing a Roslyn analyzer to raise a diagnostic when a certain library method is used within a certain method in a certain kind of class, but I cannot retrieve the symbol in the parent or ancestor syntax nodes. For example, class C { void…
meng
  • 441
  • 3
  • 9
6
votes
1 answer

Roslyn SyntaxTree Diff

Let's say I have two SyntaxTrees A and B, where B has been produced by applying changes to A. I would like to get the following information: SyntaxNodes & Tokens that have been removed from A to produce B SyntaxNodes & Tokens that have been added…
3dGrabber
  • 4,710
  • 1
  • 34
  • 42
6
votes
1 answer

Can we get System.Type from Microsoft.CodeAnalysis.CSharp.Syntax.TypeSyntax?

Can we get System.Type or essentially fully qualified type name from Microsoft.CodeAnalysis.CSharp.Syntax.TypeSyntax? Problem is, TypeSyntax returns the name of the type as it was written in code that may not be fully a qualified class name, we…
WSK
  • 5,949
  • 8
  • 49
  • 74
5
votes
1 answer

How to load assemblies when testing Roslyn analyzer with Microsoft.CodeAnalysis.CSharp.Testing.XUnit.AnalyzerVerifier?

Verifier has a method: public static Task VerifyAnalyzerAsync(string source, params DiagnosticResult[] expected) Is it possible to load assemblies with types which are required be source code?
Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137
5
votes
0 answers

How to write a DiagnosticAnalyzer for razor files?

My Goal My team decided, that they wanted to incorporate automation to enforce coding guidelines in .razor files. Since we're already using StyleCopAnalyzers, I might implement our own Analyzer to achieve this goal. Example To better make you…
LuckyLikey
  • 3,504
  • 1
  • 31
  • 54
5
votes
1 answer

EditorConfig - how to access editorconfig rule settings in custom analyzer

I wonder if there is a native way how to store and access additional settings for custom roslyn analyzers. Let's say I have rule with diagnostic Id 'XA0001' and in editor config I will set dotnet_diagnostic.XA0001.severity = error So far,…
5
votes
1 answer

What is the difference between various Microsoft.CodeQuality.* analyzers?

I am a bit confused about the various code analyzers for .NET Core. If you create a default .NET Core web project, you get the Microsoft.CodeAnalysis.Analyzers. In addition, on NuGet, there is Microsoft.CodeQuality.Analyzers (note the slight…
AngryHacker
  • 59,598
  • 102
  • 325
  • 594
5
votes
1 answer

How can I get class dependencies programmatically and their respective file locations?

I need to get some sort of a dependency graph between classes of a given project i.e. all the classes which that specific class uses. I want to know which classes a given class is using so that I can later find their file path within a project.…
João Paiva
  • 1,937
  • 3
  • 19
  • 41
5
votes
1 answer

Can I get project path or properties from Roslyn analyzer analysis context?

I'm implementing a Roslyn analyzer and I want to take different action based on how some properties are set within the csproj. Currently I'm accomplishing this by setting "AdditionalFiles" node in a props file imported with the analyzer. This…
Luke Schoen
  • 674
  • 6
  • 23
5
votes
1 answer

Roslyn: is ISymbol.GetAttributes returns inherited attributes

Roslyn has ISymbol interface with various useful methods. I'm trying to get all class attributes via ISymbol.GetAttributes. This is a documentation link:…
xtmq
  • 3,380
  • 22
  • 26