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
0 answers

How to write a DiagnosticAnalyzer for an embedded language using Microsoft.CodeAnalysis.Diagnostics?

I'm trying to write Code Analyzer in .NET for a custom query language that evaluates string literals that are being assigned to a type called OGraphStringQuery. I then try to identify if there are any syntax errors which then reports a diagnostic…
0
votes
1 answer

How To Use Roslyn with .NET 7

I want to write a new project with Roslyn Analyzer in .NET 7 My purpose is that to find all references for a method in a project as programmatically. I loaded the solution like the below code. _workspace =…
0
votes
0 answers

Problem with RemoveNodes when modifying C# code with roslyn API

My problem is when i call remove nodes, or even remove node. I get a ArgumentNullException. I'm trying to create a codefix that removes ".Result" and then add await. I'm stuck at the removing .Result part Here's my code private const string…
Joe
  • 685
  • 2
  • 8
  • 17
0
votes
0 answers

Referencing Cross project compilation within custom DiagnosticAnalyzer - Microsoft.CodeAnalysis.CSharp

Can anyone let me know about any way we can access cross project compilation (in same solution) inside custom DiagnosticAnalyzer. I need to analyze value of an argument for a particular method invocations. This argument could be a field from source…
0
votes
0 answers

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 download/ clone). So it would not analyse my open Visual…
0
votes
1 answer

How to get rid of warning IL2080?

In the bellow code for line ConstructorInfo constructorInfoObj = ValueType.GetConstructor(Type.EmptyTypes); I get this warning: ValueDescriptor.cs(58, 50): [IL2080] 'this' argument does…
Stefan Steiger
  • 78,642
  • 66
  • 377
  • 442
0
votes
2 answers

How to distribute analyzers as a nuget package

I have a library with a few analyzers build using the roslyn engine. This analyzer library targets netstandard2.0 Now, I want to use this analyzers library inside my other project. It targets net45. Everything works if include my analyzers…
Przemaas
  • 841
  • 11
  • 23
0
votes
0 answers

From rulesets to .editorconfig

We want to migrate from rulesets to .editorconfig. In one repository we have one ruleset file with some minimal quality gate defined centrally for all repos and several others, which are either component- or product code-/test code-specific. All…
mato_lo
  • 1
  • 2
0
votes
1 answer

How to get defined preprocessor symbols for given compilation in roslyn analyser

Inside roslyn diagnostic (analyzer) - is there a way to know what preprocessor symbols are defined for a current compilation?
Przemaas
  • 841
  • 11
  • 23
0
votes
1 answer

Using Source Generator to create simpler method overload

I got a class that currently needs to be registered like this (to work reflection-free) ViewLocator = new StrongViewLocator() .Register(new ViewDefinition(typeof(MainWindow), () => new MainWindow())) …
Etienne Charland
  • 3,424
  • 5
  • 28
  • 58
0
votes
0 answers

Roslyn - How to create a CodeFix to mark a class as 'sealed'

I have created an Analyzer rule to throw a Warning when a class has not been inherited but is not marked as sealed. The analyzer itself is working as expected, but I am unable to get the CodeFix to work. I am not even presented with the option to…
user3036519
  • 105
  • 1
  • 11
0
votes
0 answers

Roslyn codefix and fixall action not executed properly under unit test

I've 'successfully' written a CodeFix and FixAllProvider, BUT... The diagnostic I'm trying to handle can occur multiple times in the same document on the same line. However, the behavior under unit test (CSharpCodeFixTest) stumps me. If a test…
0
votes
0 answers

How do you import the required references to execute C# code stored in string with CSharpScript.EvaluateASync?

Context: I am trying to create a VSTO add-in for PowerPoint. A function I want to create is for users to click a button -> call an API -> return code that can be executed and carry out changes in powerpoint (e.g. add textbox, formatting). To do…
Alex
  • 11
  • 2
0
votes
1 answer

Is there any way Csharp scripting API could infer types of arguments without having to specify it?

I have the following piece of code: string actionString = "(p1,p2)=>p1.HP-=20"; var options = ScriptOptions.Default.AddReferences(typeof(SimplePlayer).Assembly).AddImports("Player"); var script = CSharpScript.Create
0
votes
0 answers

.target file is not applied to the .csproj file when nuget package is released

I'm making a nuget package which only includes references to a bunch of analyzers and a ruleset file. I am struggling with getting it to add the tag to the .csproj file during package install. After searching for a few hours I…
Asser
  • 109
  • 1
  • 9