### Install Banned API Analyzer via NuGet Source: https://github.com/dotnetanalyzers/bannedapianalyzer/blob/master/README.md Use this command to install the Banned API Analyzer package into your project. Ensure your NuGet client supports Semantic Versioning 2 for prereleases. ```powershell Install-Package DotNetAnalyzers.BannedApiAnalyzer ``` -------------------------------- ### Example BannedSymbols.txt format Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt This file lists symbols to ban, optionally with a semicolon-separated message. Supports type (T:), method (M:), and property (P:) designations. ```text # BannedSymbols.txt (place next to the .csproj file) T:System.Console;Use ILogger instead M:System.GC.Collect;Do not force GC collection T:System.Threading.Thread;Use Task/async patterns P:System.Environment.CurrentDirectory;Inject IHostEnvironment instead ``` -------------------------------- ### Install Banned API Analyzer using .NET CLI Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt Install the analyzer package using the .NET CLI. This package is added as a development dependency. ```powershell dotnet add package DotNetAnalyzers.BannedApiAnalyzer ``` -------------------------------- ### RS0031 - Compiler output for duplicate banned symbol Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt Example compiler output indicating a duplicate banned symbol (RS0031) and pointing to both the duplicate and original entries in `BannedSymbols.txt`. ```text BannedSymbols.txt(2,1): warning RS0031: The banned symbols list contains a duplicate for 'System.Console' BannedSymbols.txt(1,1): (location of original entry) ``` -------------------------------- ### RS0031 - Duplicate banned symbol in BannedSymbols.txt Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt Example of `BannedSymbols.txt` containing a duplicate entry for `T:System.Console`, which triggers the RS0031 warning. ```text # BannedSymbols.txt — the second T:System.Console line triggers RS0031 T:System.Console;Use a logger T:System.Console;Use a logger ``` -------------------------------- ### RS0030 - C# code using a banned symbol Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt Example of C# code that uses a banned symbol, triggering the RS0030 diagnostic. The `BannedSymbols.txt` file must contain the corresponding ban entry. ```csharp // BannedSymbols.txt: // T:N.LegacyClient;Use HttpClient instead namespace N { class LegacyClient { } class Service { void Call() { // RS0030: 'LegacyClient' is a banned API: Use HttpClient instead var c = new LegacyClient(); // ← warning here } } } ``` -------------------------------- ### Configuring RS0030 severity via ruleset file Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt Configure the severity of RS0030 (e.g., to 'Error' or 'Warning') or suppress it by creating a `.ruleset` file and referencing it in the project's `.csproj` file. ```xml ``` ```xml StrictBannedApis.ruleset ``` -------------------------------- ### Adding BannedSymbols.txt as AdditionalFiles in SDK-style projects Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt Ensure the analyzer is active by registering `BannedSymbols.txt` as an `AdditionalFiles` item in your project file. The NuGet package usually handles this automatically. ```xml ``` -------------------------------- ### BannedSymbols.txt - Ban a parameterised constructor Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt Ban a constructor with parameters by specifying its Documentation Comment ID, including parameter types, in `BannedSymbols.txt`. ```text M:N.OldClient.#ctor(System.String,System.Int32) ``` -------------------------------- ### BannedSymbols.txt - Ban a constructor Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt Ban a default constructor using its Documentation Comment ID (`#ctor`) in `BannedSymbols.txt`. ```text M:N.OldClient.#ctor ``` -------------------------------- ### Commit Information Table Source: https://github.com/dotnetanalyzers/bannedapianalyzer/blob/master/docs/index.html This HTML template displays commit details, including SHA, message, author, committer, and parent commits. It links the SHA to the commit on GitHub. ```html
Sha1 {{>Sha}}
Message {{>Message}}
Author {{>Author.Name}} <{{>Author.Email}}> ({{>Author.When}})
Committer {{>Committer.Name}} <{{>Committer.Email}}> ({{>Committer.When}})
Parents {{>Parents}}
``` -------------------------------- ### BannedSymbols.txt - Ban a type with a migration message Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt Ban a type and provide a custom migration message by appending it after a semicolon in `BannedSymbols.txt`. ```text T:System.Threading.Thread;Use Task-based async patterns instead ``` -------------------------------- ### BannedSymbols.txt - Ban an attribute type Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt Ban an attribute type by specifying its Documentation Comment ID in `BannedSymbols.txt`. This will flag every application site. ```text T:MyNamespace.ObsoleteInternalAttribute ``` -------------------------------- ### BannedSymbols.txt - Ban a specific method overload Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt Ban a specific method overload by providing its Documentation Comment ID, including parameter types, in `BannedSymbols.txt`. ```text M:N.LegacyService.GetData(System.String) ``` -------------------------------- ### BannedSymbols.txt - Ban a property Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt Ban a property by specifying its Documentation Comment ID in `BannedSymbols.txt`. ```text P:N.Config.LegacyFlag ``` -------------------------------- ### Banning specific methods and constructors in C# Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt Use full Documentation Comment IDs to target individual method or constructor overloads for banning. This includes generic methods. ```csharp // BannedSymbols.txt: // M:N.Calc.Add // M:N.Calc.Add(System.Int32,System.Int32) // M:N.Calc.Add``1(``0,``0) ← generic method (2 type params) namespace N { class Calc { public int Add() => 0; public int Add(int a, int b) => a + b; public T Add(T a, T b) => default; void Test() { Add(); // RS0030 — M:N.Calc.Add Add(1, 2); // RS0030 — M:N.Calc.Add(System.Int32,System.Int32) Add("a", "b"); // RS0030 — M:N.Calc.Add``1(``0,``0) } } } ``` -------------------------------- ### RS0030 - Suppress single site inline Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt Suppress the RS0030 warning for a single code site using `#pragma warning disable` and `#pragma warning restore` directives. ```csharp #pragma warning disable RS0030 // Do not use banned APIs var c = new LegacyClient(); #pragma warning restore RS0030 // Do not use banned APIs ``` -------------------------------- ### RS0030 - Suppress via ruleset Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt Configure the RS0030 rule to produce an error instead of a warning by modifying the project's `.ruleset` file. This is useful for CI builds. ```xml ``` -------------------------------- ### BannedSymbols.txt - Ban a field Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt Ban a field by specifying its Documentation Comment ID in `BannedSymbols.txt`. ```text F:N.Constants.OldValue ``` -------------------------------- ### BannedSymbols.txt - Ban an event Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt Ban an event by specifying its Documentation Comment ID in `BannedSymbols.txt`. ```text E:N.Publisher.Deprecated ``` -------------------------------- ### BannedSymbols.txt - Ban a generic type Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt Ban a generic type by specifying its Documentation Comment ID with backtick notation for arity in `BannedSymbols.txt`. ```text T:System.Collections.Generic.List`1 ``` -------------------------------- ### BannedSymbols.txt - Ban an entire type Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt Configure the analyzer to ban an entire type by specifying its Documentation Comment ID in `BannedSymbols.txt`. ```text T:System.Console ``` -------------------------------- ### Conditional Rendering of Status Source: https://github.com/dotnetanalyzers/bannedapianalyzer/blob/master/docs/index.html This template logic conditionally renders 'success' or 'warning' based on the 'Status' property. It's used for displaying diagnostic statuses. ```html {{if Status !== "DisabledNoTests"}} success {{else}} warning {{/if}} ``` -------------------------------- ### Banning types and methods in Visual Basic Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt The Banned API Analyzer supports Visual Basic projects with identical rules and `BannedSymbols.txt` syntax. Symbols are resolved using `SymbolDisplayFormat.VisualBasicShortErrorMessageFormat`. ```vb ' BannedSymbols.txt: ' T:N.Banned ' M:N.Calc.Add(System.Int32) Namespace N Class Banned : End Class Class Calc Public Function Add(i As Integer) As Integer Return i End Function End Class Class Usage Sub Demo() Dim b As New Banned() ' RS0030: Banned Dim c As New Calc() c.Add(1) ' RS0030: Public Function Add(i As Integer) As Integer End Sub End Class End Namespace ``` -------------------------------- ### Fetch and Render BannedApiAnalyzer Status Source: https://github.com/dotnetanalyzers/bannedapianalyzer/blob/master/docs/index.html This JavaScript code fetches the BannedApiAnalyzer status JSON from AppVeyor API. It uses URL parameters for branch and pull request status to dynamically load data. The fetched data is then used to render diagnostics and commit information using jQuery and JsRender templates. ```javascript var urlParams = new URLSearchParams(window.location.search); var branch = urlParams.get('branch') || 'master'; var pr = urlParams.get('pr') === 'true' ? 'true' : 'false'; $.getJSON( `https://ci.appveyor.com/api/projects/sharwell/BannedApiAnalyzer/artifacts/BannedApiAnalyzer.Status.json?branch=${branch}&pr=${pr}&stream=true&job=Configuration:%20Release`, function (data) { $("#renderedDiagnostics").html($.templates($("#diagnostics").html()).render(data)); $("#renderedCommitInfo").html($.templates($("#commitInfo").html()).render(data.git)); } ); ``` -------------------------------- ### Diagnostic Table Row Template Source: https://github.com/dotnetanalyzers/bannedapianalyzer/blob/master/docs/index.html This HTML template defines a table row for displaying diagnostic information. It includes category, ID, title, severity, status, and code fix provider details. It uses helper templates for rendering icons and links. ```html {{>Category.replace("DocumentationCSharp.", "")}} {{>Id}} {{>Title}} {{>Severity}} {{>Status}} {{if CodeFixStatus === "Implemented"}} {{else CodeFixStatus === "NotImplemented"}} {{else}} {{/if}} {{if FixAllStatus === "BatchFixer"}} {{else FixAllStatus === "CustomImplementation"}} {{else}} {{/if}} ``` -------------------------------- ### Suppress RS0030 Violation Source: https://github.com/dotnetanalyzers/bannedapianalyzer/blob/master/docs/RS0030.md Use #pragma warning directives to suppress RS0030 violations for specific code sections. This is useful when a banned API must be used temporarily or in a controlled manner. ```csharp #pragma warning disable RS0030 // Do not used banned APIs obj.BannedMethod(); #pragma warning restore RS0030 // Do not used banned APIs ``` -------------------------------- ### Banning attributes in C# Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt When an attribute type is banned, RS0030 flags its application on types, members, assemblies, and modules. ```csharp // BannedSymbols.txt: T:InternalOnlyAttribute;Do not use outside platform team using System; [AttributeUsage(AttributeTargets.All)] class InternalOnlyAttribute : Attribute { } [InternalOnly] // RS0030 — attribute application class FeatureToggle { } class Service { [InternalOnly] // RS0030 — attribute on member public void Init() { } } ``` -------------------------------- ### Banning symbols in XML documentation comments in C# Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt The analyzer flags banned symbols referenced within `` and `` tags in XML documentation comments. ```csharp // BannedSymbols.txt: T:LegacyHelper class LegacyHelper { } /// /// Uses ← RS0030 fires here too /// class NewHelper { } ``` -------------------------------- ### Banning entire types in C# Source: https://context7.com/dotnetanalyzers/bannedapianalyzer/llms.txt When a type is banned, RS0030 triggers on object creation, method calls, property/field/event access, method group references, and XML documentation references to that type. ```csharp // BannedSymbols.txt: T:N.Legacy namespace N { class Legacy { public int Value; public void Run() { } public event System.Action Done; } class Usage { void Demo() { var obj = new Legacy(); // RS0030 — constructor obj.Run(); // RS0030 — method call (type banned) int v = obj.Value; // RS0030 — field access (type banned) obj.Done += () => { }; // RS0030 — event (type banned) System.Action a = obj.Run; // RS0030 — method group (type banned) } } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.