- Roslyn Cookbook
- Manish Vasani
- 277字
- 2021-07-15 17:07:40
How to do it...
- Open a C# project, say ClassLibrary, with the analyzer NuGet package System.Runtime.Analyzers.nupkg prerelease version 1.2.0-beta2 installed in it.
- Add the following source code to the project and verify that no CA1813: Avoid unsealed attributes is fired:
using System;
namespace ClassLibrary
{
[AttributeUsage(AttributeTargets.All)]
public class MyAttribute: Attribute
{
}
}
- In the solution explorer, navigate to ClassLibary | References | Analyzers, right-click on the Analyzers node and execute the context menu command Open Active Rule Set:

- In the Rule Set editor, search CA1813 in the textbox in the top-right corner.
- For the CA1813, search the result listed under System.Runtime.Analyzers, change the Action from None to Warning, and hit save:

- We should now see a CA1813 warning being reported on our attribute definition in the source code.
- In the solution explorer, verify that the project now contains that a new ClassLibrary.ruleset item, and a new CodeAnalysisRuleset property was added to the project file:
<CodeAnalysisRuleSet>ClassLibrary.ruleset</CodeAnalysisRuleSet>
- Open ClassLibrary.ruleset in a text editor outside of Visual Studio and verify that it has the following rule action specification for CA1813:
<Rules AnalyzerId="System.Runtime.Analyzers" RuleNamespace="System.Runtime.Analyzers">
<Rule Id="CA1813" Action="Warning" />
</Rules>
- Edit the ruleset file to change the ruleset Action for CA1813 from Warning to Error and save the file.
- Switch back to Visual Studio and confirm that the source code editor now shows a red squiggle and that the error list also reports an error for CA1813:

- Double-click on ClassLibrary.ruleset in the solution explorer to open it with the Rule Set editor and verify that the rule severity entry for CA1813 is now showing as Error.
- Build the project and verify that the error CA1813 is reported, confirming that the ruleset setting is preserved for command-line builds as well.
推薦閱讀
- Delphi程序設(shè)計(jì)基礎(chǔ):教程、實(shí)驗(yàn)、習(xí)題
- 基于免疫進(jìn)化的算法及應(yīng)用研究
- Cassandra Data Modeling and Analysis
- 游戲程序設(shè)計(jì)教程
- 零基礎(chǔ)學(xué)Python網(wǎng)絡(luò)爬蟲案例實(shí)戰(zhàn)全流程詳解(高級(jí)進(jìn)階篇)
- Python數(shù)據(jù)分析從0到1
- 軟件測(cè)試技術(shù)指南
- Node.js全程實(shí)例
- 軟件供應(yīng)鏈安全:源代碼缺陷實(shí)例剖析
- PrimeFaces Blueprints
- JavaScript從入門到精通(視頻實(shí)戰(zhàn)版)
- JavaScript Concurrency
- Mastering Machine Learning with R
- Game Development Patterns and Best Practices
- Python編程:從入門到實(shí)踐(第2版)