TypedPaths.Generator
1.1.0
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
<PackageReference Include="TypedPaths.Generator" Version="1.1.0" />
<PackageVersion Include="TypedPaths.Generator" Version="1.1.0" />
<PackageReference Include="TypedPaths.Generator" />
paket add TypedPaths.Generator --version 1.1.0
#r "nuget: TypedPaths.Generator, 1.1.0"
#:package TypedPaths.Generator@1.1.0
#addin nuget:?package=TypedPaths.Generator&version=1.1.0
#tool nuget:?package=TypedPaths.Generator&version=1.1.0
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
Option A: consume from NuGet (recommended)
<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 | Versions 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. |
-
net8.0
- Microsoft.CodeAnalysis.CSharp (>= 4.3.0)
- Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing (>= 1.1.3)
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 4.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.