lucaslgt.UsingGenerator 1.0.0

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

UsingGenerator

Declare a set of global using directives once, then apply it to any assembly with a single line.

C# 10 gave us global using, but no way to share one set of imports across projects. The usual workarounds — copying a GlobalUsings.cs into every project, or a shared .props file with <Using Include="..." /> items — both duplicate the list and drift. This generator moves it into a type that ships in a NuGet package.

Install

dotnet add package lucaslgt.UsingGenerator

The package carries both the attributes and the source generator.

Use

Declare a bundle — anywhere, typically in a shared library:

using UsingGenerator;

[SharedUsing("System.Linq")]
[SharedUsing("System.Text.Json")]
[SharedUsing("System.Console", Static = true)]
[SharedUsing("System.Collections.Generic.List<string>", Alias = "StringList")]
public sealed class DefaultUsingsAttribute : UsingBundleAttribute;

Apply it to an assembly:

[assembly: DefaultUsings]

That assembly now compiles as if it contained:

global using StringList = System.Collections.Generic.List<string>;
global using System.Linq;
global using System.Text.Json;
global using static System.Console;

Any project referencing the library that declares the bundle can apply it — the generator flows transitively through the package reference, so consumers install nothing extra.

Nothing happens without opting in

Declaring a bundle imports nothing, including in the assembly that declares it. An assembly gets the imports only by carrying the attribute itself. There is no ambient propagation across a project reference to surprise you.

Bundles compose

[SharedUsing("System.Text.Json")]
public sealed class WebUsingsAttribute : DefaultUsingsAttribute;

[assembly: WebUsings] emits System.Text.Json plus everything DefaultUsings carries. Duplicates across bundles are emitted once.

SharedUsing

Member Effect
SharedUsing("N") global using N;
Static = true global using static N;
Alias = "X" global using X = N;

Alias and Static are mutually exclusive — there is no such using directive.

Diagnostics

Rule Severity Raised when
UG0001 Error Alias and Static are both set
UG0002 Warning The import target is empty; the import is ignored
UG0003 Warning [SharedUsing] sits on a class that is not a UsingBundleAttribute

How it works

Attribute constructor and named arguments are constants, so they survive into assembly metadata. The generator reads the bundle attributes an assembly applies to itself, walks each bundle's base chain collecting the [SharedUsing] attributes declared on it, and emits one SharedUsings.g.cs.

Because it reads metadata rather than syntax, a bundle compiled into a referenced DLL resolves exactly like one declared in the current project. This is also why a bundle cannot carry its imports in a property or a base constructor call: neither is readable from metadata.

Repository layout

src/       UsingGenerator.Abstractions   the attributes, shipped in lib/
           UsingGenerator.Generator      the IIncrementalGenerator, shipped in analyzers/
samples/   UsingGenerator.Sample.Library declares a bundle, applies nothing
           UsingGenerator.Sample         applies it, plus a local bundle
tests/     UsingGenerator.Tests          drives the generator through a Roslyn driver
           Packaging/                    end-to-end check over a real NuGet package

Build and test

dotnet build
dotnet test
./tests/Packaging/run.ps1

run.ps1 packs the library to a local feed, packs a demo library against it, and builds a consumer that references only that demo library. It is the only check that proves the analyzer reaches a transitive consumer — the thing most likely to break silently when the packaging changes.

License

Not yet chosen; the code is all rights reserved until one is added.

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
1.0.0 150 9/5/2026