Gori.StaticInstances 1.0.3

dotnet add package Gori.StaticInstances --version 1.0.3
                    
NuGet\Install-Package Gori.StaticInstances -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="Gori.StaticInstances" Version="1.0.3">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Gori.StaticInstances" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="Gori.StaticInstances">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
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 Gori.StaticInstances --version 1.0.3
                    
#r "nuget: Gori.StaticInstances, 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 Gori.StaticInstances@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=Gori.StaticInstances&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=Gori.StaticInstances&version=1.0.3
                    
Install as a Cake Tool

Gori.StaticInstances

A Roslyn source generator that implements the static instance and/or singleton pattern via attributes, with support for Lazy and Weak references (Thread Safe or Unsafe) for instance initialization.

Installation

Add the NuGet package to your project:

<PackageReference Include="Gori.StaticInstances" Version="*" />

[StaticInstance]

Place on a constructor to generate a static instance using that constructor and the supplied arguments. Multiple attributes can be applied to the same constructor. If constructor has parameters then you must add matching arguments to the [StaticInstance] attribute, before named attribute options.

[Singleton]
public partial class DbContext
{
	private DbContext() { }

	[StaticInstance("localhost", 5432, Name = "Development")]
	[StaticInstance("staging-host", 5432, Name = "Staging")]
	private DbContext(string host, int port) { }
}

// Generated:
// public static DbContext Instance     { get; } = new();
// public static DbContext Development  { get; } = new("localhost", 5432);
// public static DbContext Staging      { get; } = new("staging-host", 5432);

[Singleton]

Is a special type of StaticInstance that is placed on a class and always uses the default constructor. Only a single Singleton attribute can exist per class. Class can still use StaticInstance attributes along side Singleton.

[Singleton]
public partial class MyService { }

// Generated:
// private MyService() { }
// public static MyService Instance { get; } = new();

Attribute Options

Options for both StaticInstance and Singleton attributes are mostly the same. To avoid confusion with passing constructor parameters to [StaticInstance], all attribute options are named.

Attribute Option Type Default Description
StaticInstance Name string None Mandatory. Name of the generated static instance
Singleton Name string "Instance" Optional. Name of the generated static instance
* Type InstanceType Property Type of static instance to create
* Internal bool false Generates with access modifier of internal instead of the default public
Singleton Constructor Constructor Auto Options for creating private default constructor if missing

All names across [Singleton] and [StaticInstance] attributes on the same class must be unique.

Option: InstanceType

Controls what type of static instance is created. If class has multiple static instances, each can be of different types.

InstanceType Description
Property Default. Creates a static read-only property
Field Creates a static read-only field
Lazy Creates a static read-only property backed by Lazy{T} field
Weak Creates a static read-only property backed by WeakReference{T} field. This will create thread-safe version
WeakLockless Creates a static read-only property backed by WeakReference{T} field

Option: Constructor

Controls when [Singleton] should create a private default constructor for the class.

Constructor Description
Auto Default. Will create constructor if class does not contain ANY existing explicit constructor(s)
On Will create constructor if class does not contain an explicit default constructor
Off Will never create a constructor

Analyzer IDs

Rule ID Severity Description
GSSI001 Error Class is missing the partial modifier.
GSSI002 Error [Singleton] used but no default constructor. Will not be raised if Constructor = On.
GSSI003 Info [Singleton] used on class with existing default constructor that is not private or protected.
GSSI004 Error Name contains invalid characters.
GSSI005 Error Multiple attributes on class with identical names.
GSSI006 Warning Two or more attributes added to the same constructor, with the same arguments.
GSSI007 Error Number of arguments defined on [StaticInstance] does not match the number of parameters on the constructor.
GSSI008 Warning An argument type added to [StaticInstance] is of a different type defined for the parameter on the constructor. Will attempt to convert.
GSSI009 Info An argument added to [StaticInstance] is null but is mapped to a value type parameter on the constructor. Will use default value.
GSSI010 Error [method: StaticInstance] added to primary constructor. Due to Roslyn issue #80511 this is not supported.
GSSI011 Error The source generator requires at least C# 11.0, or C# 13.0 when targeting .NET 9 or higher. Increase the LangVersion property in your project file.

There are no supported framework assets in this 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
1.0.3 101 8/26/2026
1.0.2 101 8/21/2026