官术网_书友最值得收藏!

  • Roslyn Cookbook
  • Manish Vasani
  • 277字
  • 2021-07-15 17:07:40

How to do it...

  1. Open a C# project, say ClassLibrary, with the analyzer NuGet package System.Runtime.Analyzers.nupkg prerelease version 1.2.0-beta2 installed in it.
  2. 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
{
}
}
  1. 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:
  1. In the Rule Set editor, search CA1813 in the textbox in the top-right corner.
  2. For the CA1813, search the result listed under System.Runtime.Analyzers, change the Action from None to Warning, and hit save:
  1. We should now see a CA1813 warning being reported on our attribute definition in the source code.
  2. 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>
  1. 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>
  1. Edit the ruleset file to change the ruleset Action for CA1813 from Warning to Error and save the file.
  2. 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:
  1. 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.
  2. Build the project and verify that the error CA1813 is reported, confirming that the ruleset setting is preserved for command-line builds as well.
主站蜘蛛池模板: 馆陶县| 宜城市| 安仁县| 河津市| 乌海市| 保德县| 洛浦县| 蒲城县| 永德县| 麻江县| 宝坻区| 石楼县| 永顺县| 齐齐哈尔市| 灵宝市| 北安市| 宜都市| 疏勒县| 天台县| 织金县| 锦州市| 安庆市| 宁强县| 海原县| 长宁区| 东至县| 宁津县| 沙田区| 衢州市| 贵溪市| 鄱阳县| 瓮安县| 囊谦县| 镇原县| 龙陵县| 垫江县| 长宁县| 清涧县| 资兴市| 永嘉县| 响水县|