csulpizi.CustomAccessibility 1.0.3

dotnet add package csulpizi.CustomAccessibility --version 1.0.3
                    
NuGet\Install-Package csulpizi.CustomAccessibility -Version 1.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="csulpizi.CustomAccessibility" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="csulpizi.CustomAccessibility" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="csulpizi.CustomAccessibility" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add csulpizi.CustomAccessibility --version 1.0.3
                    
#r "nuget: csulpizi.CustomAccessibility, 1.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package csulpizi.CustomAccessibility@1.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=csulpizi.CustomAccessibility&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=csulpizi.CustomAccessibility&version=1.0.3
                    
Install as a Cake Tool

CustomAccessibility

Provides type, namespace, and module-specific accessibility constraints to help control internal dependencies. Packaged with a code analyzer that enforces those constraints in your IDE and during compilation.

Rationale

Accessibility is an important part of strongly-typed languages to provide well-defined boundaries and abstractions for individual systems, preventing components from accessing items that are not intended to be exposed. public, internal, and private provide adequate support in most use cases, but are not flexible enough to cover every design pattern.

A good example is the "Chain-of-Responsibility" design pattern. In this pattern, we expect classes to only interact with their subordinate.

Imagine we have the classes Lieutenant, Sargeant, and Soldier. We expect the Lieutenant to command a Sargeant, and a Sargeant to command a Soldier. A Lieutenant should never issue commands directly to a Soldier.

If we define this same structure in a given project, we would need to make the Soldier class and all relevant members internal so that the Sargeant can access them. But this exposes Soldier and its members to every class in the project, not just Sargeant, breaking the pattern we are trying to achieve.

A similar problem appears when testing. We may want a method to be private, but in order to unit test the method in a test project we need to use [assembly: InternalsVisibleTo("TestProject")] and mark the method as internal. This again breaks the desired pattern, since any type in the project can now access the method we wanted to be private.

Usage

Using the OnlyAccessibleBy attribute on any definition allows us to constrain which types and namespaces are allowed to access that definition.

using CustomAccessibility.Attributes;

[OnlyAccessibleBy("Sargeant")]
class Soldier
{
    ...
}

[OnlyAccessibleBy("Lieutenant")]
class Sargeant
{
    ...
}

[OnlyAccessibleBy("Main")]
class Lieutenant
{
    ...
}

The OnlyAccessibleBy attribute can be used on classes, interfaces, structs, enums, methods, fields, and properties. Any reference to a definition by a type that is not allow-listed will report a CACC000 - Restricted Access diagnostic error.

Internals not marked with OnlyAccessibleBy are not constrained and can be referenced anywhere in a project.

Accessibility contraints will never prevent a type from accessing its own members (ie, private access is maintained) or a type from accessing its base class's members (ie, protected access is maintained).

OnlyAccessibleBy

The OnlyAccessibleBy attribute works for both unqualified names and fully qualified names. Multiple OnlyAccessibleBy attributes are treated as "or", allowing access by multiple types. * and ** can be used for wildcard matching.

using CustomAccessibility.Attributes;

class SomeClass
{
    internal int A; // No restrictions; accessible by every type

    [OnlyAccessibleBy("Dog")]
    internal int B; // Accessible by any type named "Dog"

    [OnlyAccessibleBy(nameof(Dog))]
    internal int C; // Accessible by any type named "Dog"

    [OnlyAccessibleBy("Dog")]
    [OnlyAccessibleBy("SheepDog")]
    internal int D; // Accessible by any type named "Dog" or "SheepDog"

    [OnlyAccessibleBy("*Dog")]
    internal int E; // Accessible by any type whose name ends in "Dog" (e.g. BlackDog, SheepDog, Dog)

    [OnlyAccessibleBy("Animals.Mammals.Dog")]
    internal int F; // Accessible by the exact type "Animals.Mammals.Dog"

    [OnlyAccessibleBy("Animals.Mammals.*")]
    internal int G; // Accessible by any type in the "Animals.Mammals" namespace

    [OnlyAccessibleBy("Animals.Mammals.**")]
    internal int H; // Accessible by any type in the "Animals.Mammals" namespace or its descendents

    [OnlyAccessibleBy("Animals.Mammals.Dog.**")]
    internal int I; // Accessible by any types defined within the "Animals.Mammals.Dog" class
}

External Access

When we want to give a project access to another project's internals (e.g. [assembly: InternalsVisibleTo("TestProject")]), we can specify which definitions we want the external project to have access to.

using CustomAccessibility.Attributes;

// This method is only accessible by this project (unless CACC001 is supressed)
void Apple() {}

// This method is accessible by an external project
[ExternalAccessAllowed]
void Orange() {}

Access to external definitions is restricted by default, and will report a CACC001 - Restricted Access diagnostic error. To override this behaviour, suppress the CACC001 diagnostic.

External references will still be constrained by OnlyAccessibleBy attributes. e.g:

using CustomAccessibility.Attributes;

// This class can only be referenced by classes named "Dog",
//   whether or not that class is internal or external
[ExternalAccessAllowed]
[OnlyAccessibleBy("Dog")]
class Trainer
{
    ...
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.3 203 6/28/2026
1.0.2 692 6/28/2026