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,693
questions
0
votes
0
answers
13
views
Error CS0234 The type or namespace name 'CSharp' does not exist in the namespace 'Microsoft.CodeAnalysis'
I was about to try to make runtime scripting in UI in Unity and after adding all Microsoft.CodeAnalysis and Roslyn.Compilers packages my code still isn't compiling, saying "Error CS0234 The ...
0
votes
0
answers
64
views
.NET hot reload feels like magic, how does it work?
In .NET 6 we got the "Hot Reload" feature. C# is a compiled language, so this new feature feels like magic.
At a high level, how does it work?
And what are its limitations - can we expect ...
1
vote
1
answer
20
views
Mysterious name resolution differences between legacy and SDK-style C# projects
I encountered this confusing names-resolution problem while trying to convert legacy solution with .NET 4.6.1 projects to the new SDK-style project formats. I was able to create a minimal repro ...
1
vote
0
answers
18
views
Can a Source Generator tell if its being run for editor support (IntelliSense) or as part of an actual build?
My source generator needs to write some files to disk in addition to generating code files for the compilation in progress. These disk writes are completely unnecessary when the generator is being run ...
0
votes
0
answers
22
views
SonarSource vs. native .Net Code Analysis
I am looking for a code analysis / linting tool for C# projects. After reading about SonarSource and doing some preliminary analyses, it seems to me that many rules in the SonarSource C# ruleset are ...
-1
votes
0
answers
43
views
Is there a way to edit the dispose pattern template [duplicate]
I really like the dispose pattern suggestion in Visual studio, but it has a lot of comments which are generally not too useful for me (although I could live with that) and doesn't match my code style ...
0
votes
0
answers
20
views
How to find the path of a base class via Roslyn?
I am trying to find the path of the base class via Roslyn but I could not find any helpful resource.
Does anyone have an idea how to approach the problem.
E.g. path: "../File1.cs" contains:
...
0
votes
0
answers
49
views
C# Incremental Source Generator caching bug?
I'm trying to wrap my head around the new Roslyn Incremental Source Generators, by making a simple generator, that lists all invoked methods in a file called InvokedMethods.g.cs.
It almost works, but ...
0
votes
0
answers
24
views
Cannot access included files SourceGenerator
I added this ItemGroup to the SourceGenerator.csproj file
<ItemGroup>
<Content Include="defs\*\*.json" CopyToOutputDirectory="PreserveNewest" />
<...
2
votes
0
answers
38
views
CompilationAction in Roslyn Analyzer not running on dotnet build
I have created dotnet roslyn analyzer which run on compilation.
[System.Diagnostics.CodeAnalysis.SuppressMessage("MicrosoftCodeAnalysisCorrectness", "RS1026:Enable concurrent execution&...
0
votes
0
answers
29
views
CsharpCodeProvider target .net framework 4.8
My .net 4.8 code compiles properly and _ = "test"; doesn't throw an error when using this in visual studio. However when using CsharpCodeProvider this doesn't compile. (this can be easily ...
0
votes
0
answers
19
views
Unable to get autocomplete to work after installing vsix
I am working on a completionprovider project, which will be published as a visual studio extension. The completionprovider code is taken from https://github.com/PacktPublishing/Roslyn-Cookbook/blob/...
0
votes
1
answer
20
views
Canot resolve argument parameters in Roslyn Source Generation
I am having trouble getting parameter values from attributes in a source generator.
I have an IIncrementalSourceGenerator that is supposed to look up a class type and inspect the attributes of that ...
0
votes
1
answer
13
views
Check if nullable reference types are enabled for the project
Can I see if the nullable reference types are enabled, from the Roslyn Compilation object?
The configuration is inside .csproj, but I would prefer if it setting can be retrieved from the Compilation.
0
votes
1
answer
60
views
C# - How to guarantee that a properties remains Get only over time?
I'm implementing a piece of sensitive software and I can see a possible "failure point"/"security leak" IF in the future someone sticks a public set on a specific property.
Besides ...
0
votes
1
answer
25
views
Roslyn Source Generators Unit Testing error CS0234: The type or namespace name 'Console' does not exist in the namespace 'System'
I am trying to write a unit test for a simple Roslyn Source Generator based on the example here.
Successfully ran both MsTest as well XUnit Tests as demonstrated in the Section B of the Unit testing ...
0
votes
0
answers
38
views
Emitting only one of the generated source files
I am working with source generators and I want to generate two files containing partial methods. One file will only have defining declarations and the other will have the methods' implementation.
The ...
1
vote
1
answer
54
views
How to get type of lambda in C# using Roslyn?
I'm writing my codefixer with roslyn. I want it to work in such way:
Before:
IEnumerable<int> list = new List<int> {1, 2, 3, 4};
list = list.Where(x => x%2 == 0);
After:
...
...
0
votes
1
answer
64
views
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 ...
0
votes
1
answer
18
views
How to generate C# code from a SyntaxFactory CompilationUnitSyntax?
I am building a simple AST using the code below.
output = SF
.CompilationUnit()
.WithMembers(SF.SingletonList<MemberDeclarationSyntax>(SF.ClassDeclaration("Class1")))
....
1
vote
1
answer
68
views
c# code analyzer test is not working, but test is coorrect
I'm writting simple code analyzer and fixer using microsoft template. I want to fix nested if statements in else statement. Example:
using System;
using System.Runtime.InteropServices;
namespace ...
0
votes
1
answer
40
views
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 ...
1
vote
0
answers
99
views
error CS0012: The type ... is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=2.0.5.0
I get this error when I build the code using my fork of StackExchange.Precompilation - https://github.com/MarkKharitonov/StackExchange.Precompilation
So I took it (following the advice in https://...
0
votes
1
answer
49
views
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 ...
8
votes
2
answers
1k
views
Need to check if code contains certain identifiers
I am going to be dynamically compiling and executing code using Roslyn like the example below. I want to make sure the code does not violate some of my rules, like:
Does not use Reflection
Does not ...
1
vote
1
answer
46
views
How does the compiler pipeline correspond to Incremental Source Generation?
I'm having trouble understanding the Roslyn documentation and fiting it with what I see when working on an Incremental Source Generator.
https://docs.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/...
0
votes
1
answer
55
views
Roslyn Analyzers do not run often enough
I am writing my first Roslyn analyzers. I have basically followed the tutorial https://docs.microsoft.com/en-us/archive/msdn-magazine/2014/special-issue/csharp-and-visual-basic-use-roslyn-to-write-a-...
0
votes
0
answers
33
views
Using a type name within a static method that matches a property with the same name causes problems. Parser error?
I have a class that contains a static factory method to inspect the type provided to the factory and create an instance. The enclosing class happens to have a property called Type and for some reason, ...
0
votes
1
answer
90
views
Unity script preprocessing before compile or load with Roslyn Syntax Rewriter?
I'm looking for ways to "rewrite/tweak" C#-scripts in a Unity3d-project somewhere between me editing them in VS and the compilation before running/building the project in the Unity editor. I ...
0
votes
0
answers
46
views
Roslyn Find References to Parameter
I am trying to port a custom MsBuild Target to a Roslyn source generator.
The old code used SymbolFinder to find references to a parameter inside a methods body. In the source generator I cannot use ...
0
votes
0
answers
35
views
Is there a way to hook a custom handler to debugger events like break in .Net
I have a C# application (.NET Framework 4.8) that receives a stream of UDP packets and monitors the stream to ensure no packets are dropped (they contain sequence numbers) and that the packets arrive ...
0
votes
0
answers
37
views
How do I automatically install a nuget package from a roslyn code fixer
I try to add a nuget package reference automatically to a visual studio project if needed, with the help of a roslyn code fixer.
Since the "CodeFixProvider" is not a "Microsoft....
1
vote
0
answers
78
views
How can I read XML documentation in a C# code generator?
I've written a simple C# code generator that implements Microsoft.CodeAnalysis.ISourceGenerator that inspects an enum declaration and generates code based on members that are decorated with certain ...
0
votes
1
answer
34
views
Specific Requirements for custom Roslyn Code Analyzer to run in live analysis?
I've got a Roslyn based Code Analyzer and Codefix. When directly creating the ReportDiagnostic from an AnalyzerCodeBlock, they would show up in live analysis (Problems in Jetbrains Rider).
However, it ...
1
vote
1
answer
137
views
Generating hash of a class structure in C#
Our application makes use of serialized snapshots of state which is just a serialized version of the class at a point in time.
We have a version number on the class which we increment each time the ...
0
votes
0
answers
35
views
Getting the namespace of a Generic argument
I am trying to rewrite this code.
What gets passed in as node are instances of IStringLocalizer. They are either coming from class fields, defined as private readonly IStringLocalizer _t; getting ...
0
votes
1
answer
25
views
What's the difference between dotnet_diagnostics and equivalent rules under dotnet_style_, csharp_ etc.?
For example, in .editorconfig I can require fields to be readonly using both:
# IDE0044: Make field readonly
dotnet_diagnostic.IDE0044.severity = warning
and
dotnet_style_readonly_field = true:...
1
vote
1
answer
41
views
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 ...
0
votes
0
answers
23
views
Cannot load netstandard2.1 assembly in when adding an analyzer in Visual Studio
Lately I've been interested in writing source generators and at some point I needed to write an DiagnosticSuppressor to accommodate some generated logic. As Rider (my IDE of choice) does not support ...
1
vote
0
answers
48
views
Roslyn analyzer to detect all kinds of implicit conversions?
I need a code analyzer which warns me about every implicit cast/conversion like when assigning a variable or passing an argument to method.
I started a tutorial series on Roslyn but it's still huge ...
1
vote
1
answer
55
views
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 ...
0
votes
0
answers
22
views
How may I diagnose Intellisense's problem with my assembly?
My C# .NET 6.0 projects use my C++/CLI assembly. I can build, run, and debug them (down to unmanaged C++) with no problems.
But whenever the Release configuration is current, my Error List window ...
0
votes
2
answers
72
views
Roslyn CSharpScript EvaluateAsync Returns False When Statement Should Be True
I am just learning how to use Roslyn and I have a script like this:
var id = Guid.NewGuid();
string condition = $@"new List<Guid> {{ new Guid(""{Guid.NewGuid()}""), new ...
0
votes
0
answers
24
views
Running C# function with a class dynamically from a string [duplicate]
I am trying to dynamically execute code on the fly. The code will be in a file but for this example i've added it as a variable (code). I been trying to CSharpScript but all examples i've seen online ...
0
votes
1
answer
61
views
Can we modify the existing assembly(dll) by using CSharpCompilation.Create()? If not how to do?
I am using CSharpCompilation.Create to create assembly from scratch. The thing I got to know is this always creates a new assembly. Is there anyway to add/modify the existing DLL in Roslyn? This is ...
0
votes
1
answer
91
views
How can I make the CompletionService aware of other documents in the project?
I'm building an application that allows users to define, edit and execute C# scripts.
The definition consists of a method name, an array of parameter names and the method's inner code, e.g:
Name: ...
0
votes
1
answer
45
views
Can get script compiled with CSharpScript (Roslyn)
I'm trying to get a script compiled with Roslyn but whatever I do it keeps complaining about this, at this moment I tried almost everything and have no idea what to try next.
This is the error I get :
...
1
vote
1
answer
36
views
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 ...
0
votes
0
answers
29
views
How to combine two different context in custom Roslyn Analyzer
I'm crating custom analyzer using template "Analyzer with Code Fix (.NET Standard)" in VS 2019.
I'm investigating method members, so I'm calling in Initialize method to register actions in ...
0
votes
1
answer
47
views
Rosyln.SDK missing from Visual Studio layout
I'm creating an offline network installer for VS2019, combined with VS config export/import to mirror my VS install between two machines.
I've selected specific workflows to reduce the layout from 50-&...