SvgIconGenerator

RSCG – SvgIconGenerator    

name SvgIconGenerator
nuget https://www.nuget.org/packages/SvgIconGenerator/
link https://github.com/helluvamatt/SvgIconGenerator
author Matt Schneeberger

Generates strongly-typed C# classes from SVG icon files at compile time — access SVG icons as typed properties with no runtime file I/O.

How to use

1. Add SVG files as AdditionalFiles in .csproj:

“`xml

“`

3. Declare a partial static class with [GenerateIcons]:

“`charp

[GenerateIcons()]

internal static partial class MyIcons;

“`

3. Access icons as typed members (one per SVG file):

“`charp

Console.WriteLine(MyIcons.Circle.Name); // “Circle”

Console.WriteLine(MyIcons.Rect.InnerContent); // SVG inner XML

“`

You can use for Embedding SVG icons as compile-time constants with typed access — no file paths, no reflection, no runtime loading.

  

This is how you can use SvgIconGenerator .

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="0.0.4" include="SvgIconGenerator">
    </packagereference>
  </itemgroup>
	<itemgroup>
		<additionalfiles include="Icons/*.svg">
	</additionalfiles>

	<propertygroup>
		<emitcompilergeneratedfiles>true</emitcompilergeneratedfiles>
		<compilergeneratedfilesoutputpath>$(BaseIntermediateOutputPath)\GX</compilergeneratedfilesoutputpath>
	</propertygroup>
</itemgroup>


The code that you will use is


using DemoSvg;

Console.WriteLine(MyIcons.Circle.Name);

Console.WriteLine(MyIcons.Rect.InnerContent);



using System;
using System.Collections.Generic;
using System.Text;
using SvgIconGenerator;
namespace DemoSvg;

[GenerateIcons()]
internal static partial class MyIcons;



   The code that is generated is

namespace SvgIconGenerator
{
    /// <summary>
    /// Marks a static partial class for icon generation.
    /// The source generator will scan AdditionalFiles for SVG files matching the specified glob pattern
    /// and generate static readonly IconDto properties for each icon found.
    /// </summary>
    /// <remarks>
    /// SVG files must be added to the project as AdditionalFiles in the .csproj file:
    /// <code>
    /// &lt;ItemGroup&gt;
    ///   &lt;AdditionalFiles Include="icons/**/*.svg" /&gt;
    /// &lt;/ItemGroup&gt;
    /// </code>
    /// </remarks>
    [global::System.AttributeUsage(global::System.AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
    internal sealed class GenerateIconsAttribute : global::System.Attribute
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="GenerateIconsAttribute"> class.
        /// </see></summary>
        public GenerateIconsAttribute()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="GenerateIconsAttribute"> class.
        /// </see></summary>
        /// <param name="globPattern">
        /// Optional glob pattern to filter SVG files from AdditionalFiles.
        /// If not specified, all SVG files in AdditionalFiles will be included.
        /// Supports * (wildcard) and ** (recursive) patterns.
        /// Example: <code>"node_modules/lucide-static/icons/*.svg"</code>
        /// 
        public GenerateIconsAttribute(string globPattern)
        {
        }
    }
}

namespace SvgIconGenerator
{
    /// <summary>
    /// Represents an icon with its SVG metadata and content.
    /// This type is generated by the IconGenerator source generator.
    /// </summary>
    /// <param name="Name">The kebab-case name of the icon (e.g., "circle-user-round").
    /// <param name="DefaultAttributes">The default attributes from the SVG root element (excluding xmlns). Common attributes include: width, height, viewBox, fill, stroke, stroke-width, stroke-linecap, stroke-linejoin.
    /// <param name="InnerContent">The inner HTML content of the SVG element (paths, circles, lines, etc.).
    public sealed record IconDto(string Name, global::System.Collections.Generic.IReadOnlyDictionary<string  , string=""> DefaultAttributes, string InnerContent);
}

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace DemoSvg
{
    partial class MyIcons
    {
        /// <summary>
        /// Icon: Circle
        /// </summary>
        public static readonly global::SvgIconGenerator.IconDto Circle = new global::SvgIconGenerator.IconDto(
            "Circle",
            new global::System.Collections.Generic.Dictionary<string  , string=""> {
                { "height", "100" },
                { "width", "100" },
            },
            "<circle fill="\&quot;red\&quot;" cy="\&quot;50\&quot;" cx="\&quot;50\&quot;" r="\&quot;45\&quot;">");

        /// <summary>
        /// Icon: Rect
        /// </summary>
        public static readonly global::SvgIconGenerator.IconDto Rect = new global::SvgIconGenerator.IconDto(
            "Rect",
            new global::System.Collections.Generic.Dictionary<string  , string=""> {
                { "width", "300" },
                { "height", "130" },
            },
            "<rect fill="\&quot;blue\&quot;" ry="\&quot;20\&quot;" rx="\&quot;20\&quot;" y="\&quot;10\&quot;" x="\&quot;10\&quot;" height="\&quot;100\&quot;" width="\&quot;200\&quot;">");

    }
}

Code and pdf at

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


Posted

in

, ,

by

Tags: