TypedPaths

RSCG – TypedPaths    

name TypedPaths
nuget https://www.nuget.org/packages/TypedPaths.Generator/
link https://github.com/AlexChim1231/TypedPaths/
author Alex Chim

Turns a folder tree into strongly-typed path constants at compile time — access file/folder paths as typed properties instead of magic strings.

How to use

1. Declare which folder to scan in .csproj:

“`xml

“`

2. Access generated typed path constants:

“`charp

Console.WriteLine(TypedPaths.Src.Andrei.Value);

// strongly-typed path to the “Andrei” entry under folder “a” (e.g. “a/Andrei” or “a\Andrei” depending on OS)

“`

You can use for Eliminating magic path strings for file system operations — folder/file paths become compile-time constants with IntelliSense and refactoring safety.

   

This is how you can use TypedPaths .

The code that you start with is


<project sdk="Microsoft.NET.Sdk">

  <propertygroup>
    <outputtype>Exe</outputtype>
    <targetframework>net10.0</targetframework>
    <implicitusings>enable</implicitusings>
    <nullable>enable</nullable>
  </propertygroup>
	<itemgroup>
		<packagereference version="1.3.0" include="TypedPaths.Generator">
	</packagereference>
	<propertygroup>
		<emitcompilergeneratedfiles>true</emitcompilergeneratedfiles>
		<compilergeneratedfilesoutputpath>$(BaseIntermediateOutputPath)\GX</compilergeneratedfilesoutputpath>
	</propertygroup>
	<itemgroup>
		<typedpathsfolder include="a" classname="Src">
		<!--<TypedPathsFolder Include="template" />-->
	</typedpathsfolder>
</itemgroup>


The code that you will use is


Console.WriteLine("Hello, World!");
Console.WriteLine(TypedPaths.Src.Andrei.Value);


   The code that is generated is

// <auto-generated>

namespace TypedPaths;
public static class Src
{
    public const string Value = "a";

    public static class Andrei
    {
        public const string Value = "a/andrei.txt";
    }
}

namespace TypedPaths 
    {
        [System.AttributeUsage(System.AttributeTargets.Class)]
        internal class TypedPathAttribute : System.Attribute { }
    }

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/TypedPaths


Posted

in

, ,

by

Tags: