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.
2,833
questions
0
votes
1
answer
44
views
Fatal error. Internal CLR error. (0x80131506) when building projects using `dotnet build`
When building or running tests in any C# projects from a PowerShell command line (dotnet build or dotnet test) I get a "Fatal error. Internal CLR error" with the following output:
MSBuild ...
0
votes
1
answer
24
views
RoslynAnalyzer throws casting errors during build
I'm trying to build NLog in Visual Studio 2019 (Community edition, if that's relevant). NLog is not building due to the casting error below. I've searched the code for the "TypeParameterSymbol&...
0
votes
0
answers
20
views
C# Compilation to be done from a EDITOR with JSON Support (Serialize and Deserialize)
I am here after spending weeks trying to resolve the below issue.
I have c# code which is written in the front end editor.
We use Monaco Editor on the front end to write the code.
That code is ...
2
votes
1
answer
53
views
Generate a 'readonly record struct' using Roslyn
I can't generate a readonly record struct using Roslyn.
My code:
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
var tree = SyntaxFactory.CompilationUnit()
.AddMembers(
...
0
votes
1
answer
32
views
How to add "details" into dynamically compiled DLL with Roslyn?
I am compiling and loading C# code in run-time using Roslyn. I would like to add properties/details into the resulting file. How can it be done?
Example:
1
vote
1
answer
41
views
CIL shows `box` ops when the generic type is constrained to a class
I have the following method:
public static bool EquivalentTo<T>(this T? current, T? compare)
where T : class
{
if (current is null && compare is null)
// both are null
...
0
votes
1
answer
28
views
how do I actually reference roslyn code analysis for runtime?
So I want to do some things which require inspecting my solution files and projects. Roslyn seems like the way to go - so I find some sample code:
MSBuildLocator.RegisterDefaults();
var workspace ...
0
votes
0
answers
27
views
How can I detect if C# compiler (Roslyn) is running to get IDE diagnostics on VSCode instead of explicit dotnet building?
Is there a csproj target or property that I can use to differentiate the automatic compilation that Roslyn LSP runs to give me diagnostics on VSCode from actual explicit build compilation?
I need to ...
-1
votes
1
answer
58
views
How to use Banned API analyzers in own code
Roslyn has banned analyzers feature. I wanna use in my own code that compile code dynamically (own scripting engine based on C# lang) via CSharpCompilation class with WithAnalyzers extension method. I ...
2
votes
1
answer
141
views
Creating executable assembly at runtime
How can I compile a single code, as a string, into a working and standalone .exe file? I tried three different things:
CSharpCodeProvider: This works fine, but I need it for newer .NET 7 and this ...
0
votes
0
answers
32
views
Roslyn semantic model GetTypeInfo on method invoke argument is null
I'm trying to analyze something like blah.SomeMethod(x => x.PropertyA) in a method call and get the class of 'x' and property referenced so I can go verify that property has an expected attribute ...
2
votes
1
answer
26
views
In a C# Roslyn Analyzer, can I detect if a given Diagnostic is enabled?
I'm writing a Roslyn analyzer which detects certain patterns in the code that we don't like, and raising a Diagnostic (the specifics are irrelevant here).
However, detecting said patterns is ...
1
vote
1
answer
35
views
What's the design decision for differentiating leading and trailing trivia in Roslyn
In Roslyn, each syntax token has associated leading and trailing trivia with it. What was the decision of adding both leading and trailing trivia, instead of, say, sole leading trivia and an EOF empty ...
0
votes
0
answers
43
views
How to use the OpenAIClient in a Roslyn Analyzer without DiagnosticScopeFactory blocking it?
Im trying to write a Roslyn Analyzer which searches in code comments for some kind of hashtags and generates a Gpt prompt from it using the codefix lightbulb.
However when sending the gpt request I ...
1
vote
1
answer
50
views
Why does nullability check not work with value types?
Given the following code samples:
class Value { }
Value? value = null;
if (value is not null)
{
Value value2 = value;
}
int? value = null;
if (value is not null)
{
int value2 = value; // ...
0
votes
0
answers
14
views
Finding all cases where Project A references Project B with Roslyn?
Suppose I have 100 classes in Project A, and 50 of them reference classes in Project B.
Is there a way to ask Roslyn to find a list of all these 50 classes? Or am I forced to load every class in ...
0
votes
2
answers
39
views
How to get directly referenced projects in a Roslyn analyzer?
I have some C# projects that should have some configurations in the project that references them. I'd like to write a Roslyn analyzer(DiagnosticAnalyzer) to check if those directly referenced projects ...
12
votes
2
answers
1k
views
Roslyn warning compiling .Net Framework app
The following warning has suddenly turned up on my C# .Net Framework 4.8 app.
I have deleted all bin and obj folders, and I've tried removing and installing the DotNetCompilerPlatfrom package, but the ...
0
votes
1
answer
22
views
Get shared project ".shproj" from a solution with Roslyn
I have a solution where I have a Shared Project of extension ".shproj"
I need to add a class to the project with Roslyn, but when I look in the list of projects, It does not contain the ...
1
vote
1
answer
58
views
How Do I Transplant and Execute User Code in a C# Source Generator?
I am creating a Roslyn .NET source generator. With it I am inspecting user code for lambda expressions given as an argument in a method call like this:
// User Code
MyLibrary.MyMethod<Guid>(k =&...
1
vote
3
answers
659
views
How to remove or turn off Roslyn Analyzer in VS Code?
Maybe it's from the recent VS Code update, It suddenly pops up..
it's highlighting some of my non-error codes in a blue line and sometimes it translucent some of my variables that doesn't have an ...
2
votes
1
answer
47
views
Roslyn: SyntaxTree compilation fails, whereas string from SyntaxTree compilation works [duplicate]
[Edit] Solution:
Problem were solved by below answers. I used this part of code:
SyntaxFactory.IdentifierName(
SyntaxFactory.Identifier(
SyntaxFactory.TriviaList(),
SyntaxKind....
0
votes
0
answers
13
views
How to reference project in a Roslyn analyzer without nuget?
I'm writing a Roslyn analyzer/code generator.
It has to deserialize a Json.The classes used for this are also used by another project so create said Json.
The idea was to put all these classes into a ...
0
votes
1
answer
28
views
Source Generator Running on Indirectly Associated Projects
I have built an incremental source generator and packaged it into a nuget package to be consumed by a different solution. I have referenced it in my 'Models' project. The issue is that the Models ...
1
vote
1
answer
89
views
How can I parse C# code snippets using Roslyn?
I am trying to parse C# code snippets using Microsoft's Roslyn. Example code:
var tree = CSharpSyntaxTree.ParseText(@"
class TestClass
{
void TestMethod ()
{
int i;
}
}")...
0
votes
1
answer
37
views
In Roslyn ToMinimalDisplayString method, what does 'position' argument mean?
I'm looking at this method in ISymbol:
string ToMinimalDisplayString(SemanticModel semanticModel, int position, SymbolDisplayFormat format = null);
The documentation says that position is "A ...
1
vote
0
answers
47
views
Roslyn compiler - adding reference I built at runtime with MetadataReference.CreateFromImage tries to load dll from filesystem
TL;DR in the end of question, full details below:
I have two C# scripts I want to build with Roslyn.
First script define some classes and types, the second one uses them.
For simplicity lets say ...
0
votes
0
answers
153
views
Editorconfig configuration for unity methods, fields with serializefield attribute
Is there any way to suppress these suggestions on unity event methods and fields with [SerializeField]? I'm currently using roslyn analyzer on vscode.
Configure editorconfig under [*.unity.cs] and ...
0
votes
1
answer
24
views
How to configure/remove fields from the "Configure your new project" when creating a new project template in visual studio
How can I remove certain fields from the "Configure your new project" when creating a new project template in visual studio?
For an example, there is a Framework section where you can choose ...
1
vote
1
answer
79
views
Why shouldn't I read files in a SourceGenerator?
I'm creating a SourceGenerator that will read in a YML file defining the different states of a fluent API and then generate the relevant interfaces to implement the grammar.
I've just seen a warning ...
0
votes
1
answer
90
views
What exactly is represented by the "Compilation" object provided in a Roslyn IIncrementalGenerator?
According to the Incremental Generators documentation, an incremental generator will be called exactly once by the host regardless of how many projects are involved in the compilation:
...
0
votes
0
answers
36
views
How can I get the NUnit CodeFixVerifier to correctly test winforms code?
I am writing a Roslyn analyzer which must (unfortunately) do some analysis on code using winforms. I am having trouble writing unit tests for this analyzer because I cannot get the test state to ...
0
votes
0
answers
61
views
How to perform logging in C# Incremental Source Generator?
I'm building a new C# Source Generator and I couldn't find any proper documentation on how to do logging/outputting of diagnostics information. Is it possible? Otherwise, what's the current ...
0
votes
0
answers
44
views
Flaky `ApplicationSyntaxReference` on `AttributeData` with (Incremental) Source Generator
Unfortunately, I have no minimal sample of the issue, but I've pushed the repo publicly, where it should be reproducable: https://github.com/Yeah69/ContainerFeatureSampleComparison
I've implemented a ...
0
votes
1
answer
70
views
Error CS0012 The type 'ValueType' is defined in an assembly that is not referenced
I have two projects that both reference a local project in the same solution named Morris.Moxy, both of these apps are .NET 7.
When I compile my Domain project, Morris.Moxy and Domain both compile ...
0
votes
0
answers
44
views
Filtering ClassDeclarationSyntax inheriting from generic type
I'm writing a source generator that adds code to partial types inheriting from a specific generic type (that is also generated). I'm using IIncrementalGenerator and I'm struggling with the predicate ...
1
vote
1
answer
80
views
Using an in-memory assembly as metadata reference in another in-memory assembly in .NET
I'm using Roslyn to compile strings of C# code. I'd like to use the output of one of the compilations as metadata reference for another, but when I try to invoke a method that forces the loading of ...
0
votes
0
answers
104
views
How to Make C# Source Generators Create Files in Two Projects Simultaneously?
I have 3 projects.
Project 1: Simple Dotnet MAUI App.
Project 2: A Class Library which acts as a custom NuGet Package which does some work. Lets say reads few key value pairs from a location and puts ...
0
votes
1
answer
22
views
Operation not supported. Unknown error: 0x80070057 - when tryng to run Roslyn source generator
I have created a Roslyn source code generator.
Right-click project in Solution Explorer
Edit project
Inside <PropertyGroup> add <IsRoslynComponent>true</IsRoslynComponent>
Right-...
0
votes
1
answer
265
views
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.
...
0
votes
0
answers
28
views
How to write complex ClassDeclarationSyntax generator?
I am writing code that's supposed to receive a few parameters (INamedTypeSymbols) and generate a huge class (I'm talking hundreds of lines) based on them, a class that's supposed to inherit from some ...
0
votes
0
answers
31
views
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 ...
1
vote
1
answer
187
views
Diagnostic with ID is not supported by the analyzer, but it's in the SupportedDiagnostics property
I have a C# code analyzer base class:
using System;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using CodeQuality.Shared;
using Microsoft.CodeAnalysis;
using ...
-1
votes
2
answers
262
views
How can I parse a JSON into a dynamic object allowing access to fields via dot notation using System.Text.Json?
I'm implementing a feature where a user can write an arbitrary expression that needs to be evaluated against some context. Users should be able to write expressions referring to a dynamic JSON payload....
0
votes
0
answers
43
views
The execution order and implementation details of Roslyn DiagnosticSuppressor API
Recently I have stumbled upon the DiagnosticSuppressor API in Roslyn:
https://learn.microsoft.com/en-us/dotnet/api/microsoft.codeanalysis.diagnostics.diagnosticsuppressor.reportsuppressions?view=...
1
vote
1
answer
71
views
How to implement HashCode.Combine using SymbolEqualityComparer
After updating packages to .net7 in roslyn code generation project, has error RS1024 Use 'SymbolEqualityComparer' when comparing symbols for IEqualityComparer implementation of GetHashCode()
public ...
0
votes
0
answers
57
views
Why does "dotnet_style_prefer_is_null_check_over_reference_equality_method = true" not recognize "foo == null"?
I decided to switch from foo == null checks to foo is null. Instead of having to search through all of my code, I set dotnet_style_prefer_is_null_check_over_reference_equality_method = true in my ....
0
votes
0
answers
100
views
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 ...
0
votes
1
answer
50
views
Roslyn: Auto generating methods from controller routes
I wrote a Roslyn source generator that visits all of my controllers' methods that have an HttpGet, HttpPut, HttpDelete or HttpPost attribute on them, and then makes classes like this, that I was ...
0
votes
0
answers
212
views
what is the work of roslyn csc.exe and what is the roslyn?
why it throw the error "Could not find a part of the path ... bin\roslyn\csc.exe" ?
One working project failed to compile after I made a fresh checkout. The same commit that before worked ...