812 questions
5
votes
1
answer
743
views
Disable other rules by use of a custom Roslyn Analyzer
I'm implementing a custom Roslyn Analyzer whose ruleset would override a currently existing rule shipped by default with Visual Studio. Is there some way to make my rule silence this other rule in ...
1
vote
1
answer
988
views
Can not get Roslyn to work with .NET 5 project
After spending way too much time, I still can't make Roslyn to load simple C# project.
Project source:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<...
3
votes
1
answer
1k
views
Roslyn CodeFix - MSBuild Properties/metadata and Unit testing
I'm building a roslyn analyzer/code fix but I wan't to access the MSBuild Properties and metadata (both from Directory.build.props and the .csproj) in order to know how to apply the code fix. I only ...
2
votes
1
answer
740
views
Testing source generator
I'm trying to test source generator.
Generator:
[Generator]
public class CustomGenerator : ISourceGenerator
{
public void Initialize(GeneratorInitializationContext context) { }
public void ...
1
vote
1
answer
2k
views
Get data type from PropertyDeclarationSyntax
i am working on source generator and i need to read properties data type as string
Is there any solution to get data type name such as int, string, bool, etc...?
EDIT:
var typeName = property.Type....
2
votes
2
answers
738
views
Check if a string contains valid C# code programatically in C#
I have a string that contains C# code. How can I check that the C# code in the string is valid C# and doesn't contain build errors?
I'd like to do this programmatically in C#.
I don't expect the code ...
7
votes
3
answers
2k
views
Roslyn analyzer: Is class marked as partial
Is there a way to find out whether class is partial inside Roslyn analyzer?
There is a PartialImplementationPart in IMethodSymbol, but nothing similar for INamedTypeSymbol.
I'm writing a Source ...
1
vote
1
answer
362
views
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:
...
0
votes
1
answer
348
views
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. ...
1
vote
1
answer
1k
views
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<T> and if so I want to raise a report.
eg:
class MyClass : ...
8
votes
1
answer
1k
views
Unable to add native .dll reference to source generator project
You can find the full source code at https://github.com/myblindy/GrimBuilding/tree/efcore (the efcore branch).
I understand that source generators can't automatically harvest dependencies from nuget ...
2
votes
2
answers
2k
views
C# Roslyn Compile - Use Nuget Package within dynamically compiled Code - compiles but throws an Exception at runtime (Could not load file or assembly)
I try to accomplish using a Nuget Package in dynamic compiled code using Roslyn Compiler.
I want to use a Nuget Package ( in my example https://www.nuget.org/packages/TinyCsvParser/ ) within my code. ...
1
vote
1
answer
826
views
Allow ampsersands in c# doccomments and avoid analyzer warning CS1570
I have this:
/// <summary>
/// Foo
/// </summary>
/// <seealso href="https://www.example.com/foo?q=bar&qux=2"/>
That gives:
XML comment has badly formed XML -- '...
5
votes
0
answers
847
views
How to write a Roslyn Code Analyzer to prevent bad logging practices [closed]
I would like to help prevent our dev team from not using our structured logging set up. The correct way to log (for us) would be like this:
_logger.LogInformation("This is a message template with ...
0
votes
1
answer
257
views
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 ...
3
votes
0
answers
304
views
Why doesn't my Roslyn analyzer report warnings?
I'm writing a Roslyn analyzer to check that a target type for JSON deserialization follows a few rules. In essence, if type Foo occurs in the expression JsonConvert.DeserializeObject<Foo>, then ...
1
vote
1
answer
451
views
Disable Roslyn Analyzer in debug mode
I wanted to know how do I disable all roslyn analyzers in entire solution during debug mode but keep them enable in release mode?
I can find references to disable them permanently or individually.
1
vote
2
answers
822
views
How to get all analyzers rules in a solution
I have added some analyzer via NuGet in my solution.
How to get all added analyzer rules from NuGet references?
I need the ID (e.g. CA1001) and descriptions of all my enabled analyzers.
EDIT: I need ...
0
votes
1
answer
139
views
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?
0
votes
1
answer
1k
views
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 ...
1
vote
0
answers
1k
views
SonarQube Custom c# rule using roslyn
I am using SonarQube 8.7.1 & visual studio 2019. As per requirement, we are to create a custom c# rule for sonar.
I wrote a C# Roslyn analyzer. It works fine and working as expected. When I try to ...
1
vote
1
answer
1k
views
How do I get the containing namespace of a called method using Roslyn when that method has no symbol info?
I have an application that allows users to write c-sharp code that gets saved as a class library for being called later.
A new requirement has been established that some namespaces (and the methods ...
0
votes
0
answers
156
views
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 ...
1
vote
1
answer
903
views
Rider+Unity not respecting Roslyn code analyzer ruleset
I have a Unity 2020.2.1f1 project using Rider 2019.1.3 for the IDE. I installed the Microsoft.Unity.Analyzers (1.10.0) for it by basically following the tutorial published by Unity (https://docs....
0
votes
1
answer
703
views
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 -> ...