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

Is it possible to access appsettings.json from a Roslyn Analyzer?

The title says it all really. I've tried using RegisterAdditionalFileAction in a DiagnosticAnalyzer but it doesn't seem to include the appsettings*.json file(s). Is it even possible? I'd like to write an analyser that would check those files for…
Steve Wilford
  • 8,894
  • 5
  • 42
  • 66
0
votes
1 answer

Invoke method of analyzed assembly from a Roslyn Code Analyzer

I have a C# roslyn code analyzer that needs to analyze the usage scenarios of generic method invocations of a given class. I am gathering all the references to the method, the generic type parameters and so forth and then want to invoke the methods…
Eike S
  • 300
  • 1
  • 11
0
votes
1 answer

Why is this Roslyn analyzer test failing?

I'm trying to understand why this test fails with: Maak.Test.InitializeNewUnitTest.NoPropertiesInitialized_Diagnostic Assert.AreEqual failed. Expected:<37>. Actual:<38>. Context: Diagnostics of test state Expected diagnostic to end at column…
grmbl
  • 2,514
  • 4
  • 29
  • 54
0
votes
1 answer

Get the approximate size of a struct in Roslyn analyzer

I want to get the approximate size of a struct that is passed as a parameter to a method. I am using a Roslyn analyzer. We want to check that structs passed in by value are not larger than 128 bytes. The use of sizeof or Marshal.SizeOf is sufficient…
Tomáš Oplatek
  • 220
  • 3
  • 10
0
votes
1 answer

How to get all interfaces of a class in roslyn analyzer

I want to analyze whether a class implements a specific interface, concrete I am looking if the class implements IEquatable and if so I want to raise a report. eg: class MyClass : IEquatable { ... } Until now I was not…
Christoph
  • 438
  • 1
  • 6
  • 14
0
votes
1 answer

How to generate/edit typescript code in c# using roslyn

I have a typescript file which I want to edit dynamically from code. I want to pass the path of the typescript file to some sort of AST parser and then do manipulation on that AST and Save it. However, all this I want to do using C# code since I…
0
votes
1 answer

Allow ampsersands in c# doccomments and avoid analyzer warning CS1570

I have this: /// /// Foo /// /// That gives: XML comment has badly formed XML -- 'Reference to undefined entity 'qux'.' csharp(CS1570) The problem is the & is invalid…
lonix
  • 14,255
  • 23
  • 85
  • 176
0
votes
1 answer

Rosyln/NetAnayzers/Stylecop Analyzers: Generate Inspection Report

We are using the Microsoft.CodeAnalysis.NetAnalyzer and StyleCop.Analyzer roslyn Analyzers in our solutions to check code quality. We've been asked to provide evidence that we run these analyzers to some stakeholders. Unfortunately, we only get a…
Efrain
  • 3,248
  • 4
  • 34
  • 61
0
votes
1 answer

How to ask Roslyn to generate an identifier name for me?

As the title says, how do I ask Roslyn to generate an identifier for me, similar to how the pattern matching code fixer or the generate method ones do?
Blindy
  • 65,249
  • 10
  • 91
  • 131
0
votes
1 answer

Roslyn - obtain reference to a string assigned to property

Throughout Visual Studio Solution with multiple projects, I have code snippets similar to: sqlCommand.CommandText = "Some SQL statement"; I am able to obtain all references to CommandText being a Callee inside of a solution via: using…
Nenad
  • 24,809
  • 11
  • 75
  • 93
0
votes
1 answer

Writing a VS extension and want to get the fully qualified type of the C# text in the editor I am mousing over

I have a working VS extension in VS 2019 I trigger events when mousing over text in the editor (in this case, C#) My extension receives an EventArgs (actually MouseHoverEventArgs), which I can use to access the textPosition, and can get the line…
0
votes
0 answers

Roslyn semantic analysis: Why is there a difference when invoked from a unit test?

Consider the code below. MySolutuion.sln using System; using System.Linq; using Microsoft.Build.Locator; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.MSBuild; public class MyApp { …
momo
  • 3,313
  • 2
  • 19
  • 37
0
votes
1 answer

Is it impossible to have NuGet-dependencies at .nuspec of a Roslyn analyzer package?

Scenario 1 (Good): Create new VisualStudio solution by Class-Library (.Net Standard 2.0) template. Install any nuGet-package to it (e.g. Newtonsoft.Json) Right click on project, Properties -> Package -> Mark "Generate NuGet package on build" Build…
user1234567
  • 3,991
  • 3
  • 19
  • 25
0
votes
0 answers

C# roslyn code analysis: how to identify type definition source code?

I parse a syntax tree to find method return type. Now how to identify the source class definition of the type? I need a sort of "go to definition" sample code. Any suggestion? Thanks
Claudio
  • 133
  • 2
  • 11
0
votes
0 answers

How to use syntax record declaration with members

I'm trying generate c# 9 record using roslyn. but, having problem to create a simple record. What i'm trying to achieve is public record Student(string FirstName, string LastName); I created. var recordDeclaration =…
Tun
  • 824
  • 3
  • 16
  • 30