问题标签 [roslyn-code-analysis]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
c# - 如何获取代码分析器在 roslyn 中发现的所有问题?
我正在使用新的 roslyn 的代码分析器功能。我有一个规则集,只有一条规则CA1008 : Enums should have zero value.
。此规则来自Microsoft.AnalyzerPowerPack
分析器。
当我在 Visual Studio 中运行代码分析时,它声称
警告 CA0064 CA0064:未执行分析,因为无法加载指定的规则集或不包含任何托管代码分析规则。
当我打开某个文件时,警告通常会弹出到错误窗口中。如何强制 VS 为我提供项目中所有文件的结果,而不仅仅是打开的文件?
c# - 如何在从标记接口继承的接口中检查功能?
至于定义 Roslyn Analyzer 规则的学习经验,假设我定义了如下接口
我如何检查从标记接口继承的所有接口(甚至可能实现这些接口的类和从实现类继承的类,但现在是次要的)的函数ISomethingRoot1
签名ISomethingRoot2
?我看到至少有一个问题与此有关Find all class declarations than inherit from another with Roslyn,但我没有掌握它的窍门。看起来我应该注册到SymbolKind.NamedType
操作,但是找到从andAnalyzeSymbol
继承的函数签名的部分会是什么样子?ISomethingRoot1
ISomethingRoot2
这是我目前正在做的一些代码,以使问题更清楚:
c# - 为什么我无法获取 Lambda 表达式的返回类型?
我有一个需要检查 Lambda 表达式的返回类型的分析器。
在 VS Syntax Visualizer 中,我可以右键单击 aParenthesizedLambdaExpression
并单击Show Symbol并查看正确的返回类型。
我尝试在代码中复制它,如下所示:
roslyn - 使 Roslyn 分析器可配置的最佳方法是什么?
我正在为 Roslyn 制作分析仪。我正在做的是一种诊断方法,它发现方法太长了。我想让任何被认为“太长”的可配置,最好是整个解决方案或项目的一种配置。解决此问题的最佳方法是什么?
我想到的唯一选择是在程序集中搜索特定的配置属性。这将需要解决方案中每个项目的属性。它还要求诊断的用户引用特定于定义此属性的诊断的库。
这是一个好主意,还有哪些其他选择?
c# - 如何使用 roslyn (CodeFixProvider) 添加新区域
我想#region
使用 CodeFixProvider 在我的代码中添加一个新的。我已经知道区域是节点的主要琐事的一部分。
但是我怎样才能创建一个新的RegionDirectiveTrivia
?
c# - Reliably compare type symbols (ITypeSymbol) with Roslyn
I am trying to reliably compare two instances of ITypeSymbol
the easiest and most straight forward way possible in the following situation (I came across these issues in a bigger project and tried to simplify it as much as possible):
I have got a CSharpCompilation with this SyntaxTree:
We are walking through the tree with a CSharpSyntaxRewriter
, changing the class and updating the Compilation
. In the first run we remember the ITypeSymbol
of the first constructor parameter (which is the type of the class itself in this case).
After updating the compilation we are calling the same rewriter again and obtaining the ITypeSymbol from the constructor parameter a second time.
After that, I compare the two ITypeSymbols which I expect to represent the same type MyAssembly.Foo
.
My first comparison approach was just calling the ITypeSymbol.Equals()
method, but it’s returning false
. It basically returns false
because we changed the compilation and got a new SemanticModel
in the meantime. If we don’t do this, the Equals() method actually returns true.
Comparing the DeclaringSyntaxReferences
(as also stated here How to compare type symbols (ITypeSymbol) from different projects in Roslyn?) returns false because we changed the class Foo
itself in the meantime. The behaviour would be the same if the constructor parameter would be of type Bar
and we rewrote Bar
. To verify this, just uncomment the line
and replace the constructor parameter type by Bar
in the code sample.
Conclusion:
ITypeSymbol.Equals()
doesn’t work with a new compilation and semantic model and comparing the DeclaringSyntaxReferences
doesn’t work with a type we changed in the meantime.
(I also tested the behaviour with a type of an external assembly – in this case ITypeSymbol.Equals() worked for me.)
So my questions are:
- What’s the intended way to compare types in the described situation?
- Is there a single catch-all-solution or do I have to kind of mix/combine different approaches to determine type equality (maybe also taking the string representation of the fully qualified name into account)?
This is the full test program with which the issue is reproducible for me. Just copy, include Roslyn references and execute:
c# - 为 CSharpCompilation.Emit 指定框架版本
有谁知道在使用 CSharpCompilation 的扩展方法 Emit 发出程序集时是否可以指定框架版本?
我们显然需要以 4.5.1 为目标,否则我们会得到一个令人讨厌的运行时错误:
还是我们必须等到客户端升级到 Framework 4.6 才能使用 Roslyn?
c# - Roslyn 忽略子树
我正在尝试使用 Roslyn 并通过扩展CSharpSyntaxWalker
和覆盖其Visit...
方法来实现我自己的节点访问者。但我有一个问题。
考虑以下 C# 类:
我只需要对MyClass
. 我可以获得对该节点的引用。所以我可以获得对SyntaxNode
代表的引用MyClass
,这样我就可以做到:
但是,访问将对GetSomething
方法做出反应,我想将其排除在访问之外。其实我想排除MyClass2
. 因此,在我的自定义 walker 中,我可以覆盖访问方法以对方法做出反应,但是当我遇到类声明时,我想指示访问者忽略该节点。
我怎样才能做到这一点?
c# - 检查 argumentSyntax 是否有效以使用具有 IParameterSymbol 的方法调用
我有两种方法:
和2类:
我使用 Roslyn 分析的代码:
我想分析 Method1 Call 并检查它是否也是有效的 Method2 Call,意思是:
我到了从语义模型(IParameterSymbol)中获得 Method2 的地步,并且对于调用中的每个参数,我都有 ITypeSymbol。如何检查参数类型是否对此 IParameterSymbol 有效?(继承和隐式转换)?