Questions tagged [roslyn]

Roslyn (aka .NET Compiler Platform) provides open-source C# and Visual Basic compilers with rich code analysis APIs. It enables building code analysis tools with the same APIs that are used by Visual Studio.

Written in 100% managed code, the Roslyn project exposes the C# and VB compilers as a service.

Historically, the managed compilers we’ve shipped in Visual Studio have been opaque boxes: you provide source files, and they churn those files into output assemblies. Developers haven’t been privy to the intermediate knowledge that the compiler itself generates as part of the compilation process, and yet such rich data is incredibly valuable for building the kinds of higher-level services and tools we’ve come to expect in modern day development environments like Visual Studio.

With these compiler rewrites, the Roslyn compilers become services exposed for general consumption, with all of that internal compiler-discovered knowledge made available for developers and their tools to harness. The stages of the compiler for parsing, for doing semantic analysis, for binding, and for IL emitting are all exposed to developers via rich managed APIs.

Useful resources

Tips & Tricks

2826 questions
1
vote
0 answers

How get all interface implementations from IAssemblySymbol?

I have a method to get all interface implementations from Assembly: public static class AssemblyExtensions { /// /// Gets all implementations of interface or class from assembly /// ///
user606521
  • 14,486
  • 30
  • 113
  • 204
1
vote
0 answers

Retrieve Semantic Model / Source of solution references of Roslyn Source Generators

I am currently writing a source generator. This source generator should not only take into account the types and syntax trees in the project it's added to, but also the projets the target project…
Eike S
  • 300
  • 1
  • 11
1
vote
1 answer

Difference in performance when using Compiler API versus Workspace API in Roslyn

I know that there are several API provided by Roslyn; however, I was not sure what is the difference in performance when using Compiler API and Workspace API. Does Workspace API build the entire solution and generate the IL code?
ce8
  • 23
  • 1
  • 3
1
vote
1 answer

Source Generator Testing

I'm trying to develop a source generator that I can theoretically add to my projects, and for the given project have it find classes that are marked up with a specific attribute, and then build a corresponding generated file for each. I've set up…
Ronnyek
  • 482
  • 5
  • 16
1
vote
1 answer

c# code analyzer test is not working, but test is correct

I'm writing a simple code analyzer and fixer using the Microsoft template. I want to fix nested if statements in else statement. Example: using System; using System.Runtime.InteropServices; namespace ExamApp { public class Program { …
Kre4
  • 95
  • 5
1
vote
1 answer

How to make c# analyzer from CLI, to warn on usage of ! operator

Ok, as usual the msft docs are utterly useless for me. Here is my Q: How can I make an analyzer using the CLI (dotnet 6.0 SDK on linux) and have it run when I dotnet build other projects, and have it print warnings if the null forgiveness operator…
KP99
  • 378
  • 1
  • 9
1
vote
2 answers

What's the best way to check source code lines for exceeding line length with Roslyn Analyzer?

I'd like to enforce some hard limits to code line length. What Roslyn's API should I use for it? Registering action for every syntax node and checking node's location seems to be not the most efficient approach.
Pavel Voronin
  • 13,503
  • 7
  • 71
  • 137
1
vote
1 answer

How to find the relative file path of a class file to csproj in SourceGenerator

I am using SourceGenerator and analysing a class to create a string that needs to have the relative path of the class file to the csproj file. I am able to find the absolute path of the class file using code below, but not the relative path public…
resp78
  • 1,414
  • 16
  • 37
1
vote
1 answer

How can I get the comments for symbols referenced by a CompletionItem?

Coming from this question, I've managed to get all CompletionItem instances available for a specific offset using completionService.GetCompletionsAsync(document, offset);. So, after querying for completions of "MyString".Len, I get a CompletionItem…
mike
  • 1,627
  • 1
  • 14
  • 37
1
vote
1 answer

Why does CSharpExtensions.ClassifyConversion return an explicit reference conversion for object / string?

The CSharpExtensions.ClassifyConversion(Compilation, ITypeSymbol, ITypeSymbol) method returns an explicit reference conversion if called with the special type object as source and the special type string as destination argument: var objectTypeSymbol…
latonz
  • 1,601
  • 10
  • 21
1
vote
0 answers

Unable to retreive XAML members of Control via Roslyn Compilation

Given an INamedTypeSymbol of a WPF Window, I can't seem to obtain a ISymbol for the members that are defined in XAML (and are then compiled as part of the auto-generated .g.cs file). To reproduce the problem I'm having, Create a new blank WPF…
1
vote
0 answers

How to hide a local DLL reference in Analyzer Dependencies and only have just analyzers

I have created 2 custom analyzer using template "Analyzer with Code Fix (.NET Standard)" in VS 2019. Each analyzer has different project, and package project as well. Also I have Common project with some common shared code that are using every…
1
vote
3 answers

How to completely evaluate an attribute's parameters in a C# source generator?

In a source generator, I've found an attribute on a class and resolved its FQN with GeneratorSyntaxContext.SemanticModel to, e.g., deal with its name being written with or without "Attribute" in it. How can I resolve the arguments? Basically I want…
lcsondes
  • 484
  • 4
  • 16
1
vote
0 answers

How to replace aspnet_compiler.exe with Roslyn?

This question is related to How to speed up aspnet_compiler.exe? The answer mentions using Roslyn instead and provides the link to this post -…
mark
  • 59,016
  • 79
  • 296
  • 580
1
vote
2 answers

Compiling New C# Versions (C# 7.0 Higher) From The Command Line

When I try to compile source code from command line with Microsoft's Visual C# Compiler (v4.8.4084.0), I get the following error and/or warning: PS C:\> csc Program.cs This compiler is provided as part of the Microsoft (R) .NET Framework, but only…
Sercan
  • 4,739
  • 3
  • 17
  • 36
1 2 3
99
100