ScopeGuard 0.1.1

dotnet add package ScopeGuard --version 0.1.1
                    
NuGet\Install-Package ScopeGuard -Version 0.1.1
                    
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="ScopeGuard" Version="0.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ScopeGuard" Version="0.1.1" />
                    
Directory.Packages.props
<PackageReference Include="ScopeGuard" />
                    
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 ScopeGuard --version 0.1.1
                    
#r "nuget: ScopeGuard, 0.1.1"
                    
#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 ScopeGuard@0.1.1
                    
#: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=ScopeGuard&version=0.1.1
                    
Install as a Cake Addin
#tool nuget:?package=ScopeGuard&version=0.1.1
                    
Install as a Cake Tool

ScopeGuard

A Roslyn-based analyzer that enforces architectural boundaries at compile time.

The Problem

Many architectural patterns divide code into layers where access between them should be controlled. In practice this breaks down at one specific place: types that must be public for technical reasons, but were never meant to be used everywhere.

A common example is EF Core — entity classes must be public for the ORM to work, but that makes them accessible from any layer in the solution:

// In a UI controller — this compiles fine, but violates your architecture
var user = new UserRepository().GetById(id);
Console.WriteLine(user.PasswordHash); // Domain internals, exposed to the UI

The type is public because it has to be. But it was never meant to be used here.

The Solution

ScopeGuard introduces a [VisibleTo] attribute that lets you declare which namespaces are allowed to use a type. Any violation becomes a compile-time error — not a code review comment, not a runtime exception, a build failure.

using ScopeGuard.Attributes;

namespace MyApp.Domain
{
    [VisibleTo("MyApp.Application.**")]
    public class User
    {
        public string PasswordHash { get; set; }
    }
}

Any of the following from an unauthorized namespace now fails to build:

// In MyApp.UI.Controllers
var user = new User();                         // error SG001 — object creation
var hash = user.PasswordHash;                  // error SG001 — member access
// In MyApp.UI
class AdminUser : User { }                     // error SG001 — inheritance
class UserList : IEnumerable<User> { }         // error SG001 — generic type argument
void Handle(User user) { }                     // error SG001 — method parameter
User GetCurrent() => null!;                    // error SG001 — return type
private User _current;                         // error SG001 — field type

What Is Checked

ScopeGuard catches every place a restricted type appears:

  • Member access — calling a method, reading or writing a property or field on a restricted type
  • Object creationnew RestrictedType()
  • Inheritance and interface implementationclass Sub : RestrictedType, class Impl : IRestrictedInterface
  • Method signatures — a restricted type as a parameter type or return type, including constructors
  • Field and property typesprivate User _user, public User Current { get; set; }
  • Delegate signatures — a restricted type as a parameter or return type of a delegate declaration
  • Event typespublic event UserChanged OnChanged
  • Generic type arguments — in all of the above: List<User>, IRepository<User>, Action<User>, IHandler<Command<User>>

The attribute is placed on the type, not on individual members. All uses of the type are restricted.

Pattern Syntax

Pattern Matches
MyApp.Application Exactly that namespace
MyApp.Application.* Any single segment under Application (e.g. MyApp.Application.Handlers, but not MyApp.Application.Sub.Handlers)
MyApp.Application.** Any namespace rooted at Application, at any depth

Multiple patterns are combined with OR logic — access is granted if the caller matches any of them.

[VisibleTo("MyApp.Application.**", "MyApp.Tests.**")]
public class Order { ... }

Notes

  • [VisibleTo] can only be applied to classes and structs. It cannot be placed on methods, properties, or fields.
  • The attribute is not inherited — subclasses of a restricted type are not themselves restricted unless they carry their own [VisibleTo].
  • Same-layer references: because member signatures are checked, types in the same layer that reference each other must include their own namespace in the allowed list. For example, if User and Order are both in MyApp.Domain and Order appears in a User method signature, Order's [VisibleTo] must include "MyApp.Domain.**" alongside any other allowed namespaces.

Diagnostic Reference

ID Severity Message
SG001 Error Member '{0}' is available to '{1}', but is being accessed by '{2}'. Access denied by ScopeGuard.
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.
  • .NETStandard 2.0

    • 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.

Version Downloads Last Updated
0.1.1 91 4/26/2026
0.1.0 95 4/26/2026
0.0.3 92 4/24/2026
0.0.2 95 4/23/2026
0.0.1 107 4/23/2026