TypedPaths.Generator 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package TypedPaths.Generator --version 1.1.0
                    
NuGet\Install-Package TypedPaths.Generator -Version 1.1.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="TypedPaths.Generator" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TypedPaths.Generator" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="TypedPaths.Generator" />
                    
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 TypedPaths.Generator --version 1.1.0
                    
#r "nuget: TypedPaths.Generator, 1.1.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 TypedPaths.Generator@1.1.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=TypedPaths.Generator&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=TypedPaths.Generator&version=1.1.0
                    
Install as a Cake Tool

TypedPaths.Generator

A Roslyn source generator that turns configured folder trees into strongly typed path constants at compile time. Each configured folder becomes a nested static class (for example Src, Template) so you can avoid magic strings.

What you get

Given a structure like:

/src
  Template1.anyext
  folderA/
    Template2.anyext
  folderB/
    Template3.anyext
    Template4.anyext
/template
  email/
    welcome.txt
  sms/
    otp.txt

the generator emits one file per root folder (TypedPaths.Src.g.cs, TypedPaths.Template.g.cs, ...), each contributing to the same partial root class:

// TypedPaths.Src.g.cs
namespace TypedPaths;

public static partial class TypedPaths
{
    public static class Src
    {
        public const string Template1 = "src/Template1.anyext";
        public static class FolderA
        {
            public const string Template2 = "src/folderA/Template2.anyext";
        }
        public static class FolderB
        {
            public const string Template3 = "src/folderB/Template3.anyext";
            public const string Template4 = "src/folderB/Template4.anyext";
        }
    }
}
// TypedPaths.Template.g.cs
namespace TypedPaths;

public static partial class TypedPaths
{
    public static class Template
    {
        public static class Email
        {
            public const string Welcome = "template/email/welcome.txt";
        }
        public static class Sms
        {
            public const string Otp = "template/sms/otp.txt";
        }
    }
}

So you can use TypedPaths.Src.FolderA.Template2 and TypedPaths.Template.Email.Welcome instead of raw string paths.

Requirements

  • .NET 8 (or the TFM your project uses; the generator targets .NET Standard 2.0)
  • MSBuild / SDK-style projects

Setup

<ItemGroup>
  <PackageReference Include="TypedPaths.Generator" Version="1.0.0" />
</ItemGroup>

<ItemGroup>
  <TypedPathsFolder Include="/src" ClassName="Src" />
  <TypedPathsFolder Include="/template" />
</ItemGroup>

TypedPaths.Generator.targets (from the package buildTransitive assets) maps these folders to AdditionalFiles and Content items for generation.

Option B: local project reference (repository development)

<ItemGroup>
  <ProjectReference Include="..\TypedPaths.Generator\TypedPaths.Generator.csproj"
                    OutputItemType="Analyzer"
                    ReferenceOutputAssembly="false" />
</ItemGroup>

<ItemGroup>
  <TypedPathsFolder Include="/src" ClassName="Src" />
  <TypedPathsFolder Include="/template" />
</ItemGroup>

<Import Project="..\TypedPaths.Generator\buildTransitive\TypedPaths.Generator.targets"
        Condition="Exists('..\TypedPaths.Generator\buildTransitive\TypedPaths.Generator.targets')" />

The explicit <Import /> is important for local project reference scenarios so folder declarations are translated into AdditionalFiles consistently during local builds.

Build the project; the generator runs and adds TypedPaths.*.g.cs files to the compilation.

Usage in code

The generated type is in namespace TypedPaths, and the root class is also named TypedPaths. To avoid name clashes, use an alias:

using TypedPathsRoot = TypedPaths.TypedPaths;

// Use as const strings
string path = TypedPathsRoot.Src.FolderA.Template2;  // "src/folderA/Template2.anyext"
string emailTemplate = TypedPathsRoot.Template.Email.Welcome; // "template/email/welcome.txt"

// e.g. resolve to full path
var fullPath = Path.Combine(projectRoot, TypedPathsRoot.Src.FolderA.Template2);

Naming rules

  • Folder and file names become PascalCase identifiers.
  • Invalid identifier characters are dropped or split; leading digits get a _ prefix.
  • Duplicate names in the same scope get suffixes: _2, _3, etc.
  • Extensions are stripped from member names but kept in the path string.

Repository layout

Project Description
TypedPaths.Generator The source generator (Roslyn incremental generator).
TypedPaths.Generator.Sample Example app that uses the generator and runs a small demo.
TypedPaths.Generator.Tests Unit tests for the generator.

Build and test

dotnet restore
dotnet build
dotnet test

Run the sample:

dotnet run --project TypedPaths.Generator.Sample

License

See the repository for license information.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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.3.0 105 3/4/2026
1.2.9 90 3/3/2026
1.2.8 91 3/3/2026
1.2.7 95 3/3/2026
1.2.6 96 3/3/2026
1.2.5 94 3/3/2026
1.2.4 97 3/3/2026
1.2.1 98 3/3/2026
1.2.0 100 3/3/2026
1.1.0 105 3/2/2026
1.0.0 106 3/2/2026