BannedApiAnalyzers.Unity
1.0.2
dotnet add package BannedApiAnalyzers.Unity --version 1.0.2
NuGet\Install-Package BannedApiAnalyzers.Unity -Version 1.0.2
<PackageReference Include="BannedApiAnalyzers.Unity" Version="1.0.2"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="BannedApiAnalyzers.Unity" Version="1.0.2" />
<PackageReference Include="BannedApiAnalyzers.Unity"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add BannedApiAnalyzers.Unity --version 1.0.2
#r "nuget: BannedApiAnalyzers.Unity, 1.0.2"
#:package BannedApiAnalyzers.Unity@1.0.2
#addin nuget:?package=BannedApiAnalyzers.Unity&version=1.0.2
#tool nuget:?package=BannedApiAnalyzers.Unity&version=1.0.2
BannedApiAnalyzers for Unity
A Unity-focused fork of Microsoft.CodeAnalysis.BannedApiAnalyzers that uses Unity additional files instead of BannedSymbols.txt — no .csproj edits required.
[!NOTE]
This package is based on dotnet/roslyn-analyzers v3.11.0.
[!NOTE]
Unity additional files require Unity 2021.3 or later.
Analyzer Rules
RS0030 — Do not use banned APIs
The symbol has been marked as banned in this project, and an alternate should be used instead.
| Item | Value |
|---|---|
| Category | ApiDesign |
| Enabled | True |
| Severity | Warning |
| CodeFix | False |
RS0031 — The list of banned symbols contains a duplicate
The list of banned symbols contains a duplicate.
| Item | Value |
|---|---|
| Category | ApiDesign |
| Enabled | True |
| Severity | Warning |
| CodeFix | False |
RS0035 — External access to internal symbols outside the restricted namespace(s) is prohibited
RestrictedInternalsVisibleToAttribute enables a restricted version of InternalsVisibleToAttribute that limits access to internal symbols to those within specified namespaces. Each referencing assembly can only access internal symbols defined in the restricted namespaces that the referenced assembly allows.
| Item | Value |
|---|---|
| Category | ApiDesign |
| Enabled | True |
| Severity | Error |
| CodeFix | False |
Usage
1. Install the analyzer package
Install BannedApiAnalyzers.Unity from a package registry using either NuGetForUnity or UnityNuGet.
NuGetForUnity
- Open the NuGetForUnity window via NuGet > Manage NuGet Packages
- Search "BannedApiAnalyzers.Unity" and click Install
UnityNuGet (hosted on OpenUPM)
Install the package:
openupm add org.nuget.BannedApiAnalyzers.UnityOpen the
.asmdefof each assembly you want the analyzer to apply to, and addBannedApiAnalyzers.Unity_Unityto its Assembly Definition References.
[!TIP]
Analyzers installed via NuGetForUnity apply to all assemblies in the project (including those in the PackageCache), while analyzers installed via UnityNuGet apply only to the referenced assembly and any assemblies that depend on it.
2. Create a banned symbols additional file
Create one or more files named according to the pattern <Filename>.BannedApiAnalyzers.Unity.additionalfile
(the <Filename> part must not contain a period) and place them anywhere under Assets/:
BannedSymbols.BannedApiAnalyzers.Unity.additionalfilePlatform.BannedApiAnalyzers.Unity.additionalfile(one file per concern)
Unity automatically discovers .additionalfile files in Assets/ and passes them to the analyzer — no .csproj edits required.
For more details on Unity's additional files feature, see Additional files for Roslyn analyzers and source generators.
3. Add entries to the banned symbols file
To add a symbol to the banned list, just add an entry in the format below to one of the additional files (Description Text will be displayed as description in diagnostics, which is optional):
{Documentation Comment ID string for the symbol}[;Description Text]
Comments can be indicated with //, in the same way that they work in C#.
For details on ID string format, please refer to "ID string format".
Examples of banned symbols entries for symbols declared in the source below:
namespace N
{
class BannedType
{
public BannedType() {}
public int BannedMethod() {}
public void BannedMethod(int i) {}
public void BannedMethod<T>(T t) {}
public void BannedMethod<T>(Func<T> f) {}
public string BannedField;
public string BannedProperty { get; }
public event EventHandler BannedEvent;
}
class BannedType<T> {}
}
| Symbol in Source | Sample Entry in *.BannedApiAnalyzers.Unity.additionalfile |
|---|---|
class BannedType |
T:N.BannedType;Don't use BannedType |
class BannedType<T> |
T:N.BannedType`1;Don't use BannedType<T> |
BannedType() |
M:N.BannedType.#ctor |
int BannedMethod() |
M:N.BannedType.BannedMethod |
void BannedMethod(int i) |
M:N.BannedType.BannedMethod(System.Int32);Don't use BannedMethod |
void BannedMethod<T>(T t) |
M:N.BannedType.BannedMethod`1(``0) |
void BannedMethod<T>(Func<T> f) |
M:N.BannedType.BannedMethod`1(System.Func{``0}) |
string BannedField |
F:N.BannedType.BannedField |
string BannedProperty { get; } |
P:N.BannedType.BannedProperty |
event EventHandler BannedEvent; |
E:N.BannedType.BannedEvent |
namespace N |
N:N |
License
MIT — see License.txt.
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.