Embed.Generator 1.0.0

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

Embed.Generator

A C# source generator that embeds files directly into your assembly at compile time, inspired by C23's #embed directive.

Features

  • Embed files as ReadOnlySpan<byte> properties
  • Zero runtime I/O overhead
  • Type-safe access to embedded resources
  • Automatic path-based lookups
  • Metadata about embedded files
  • Support for both text and binary files

Installation

dotnet add package Embed.Generator

Usage

1. Mark a class with [ResourceDictionary]

using System;
using Embed;

[ResourceDictionary]
public static partial class EmbeddedResources
{
    [Embed("Resources/config.json")]
    public static partial ReadOnlySpan<byte> Config { get; }

    [Embed("Resources/logo.png")]
    public static partial ReadOnlySpan<byte> Logo { get; }
}

2. Add files as AdditionalFiles in your .csproj

<ItemGroup>
    <AdditionalFiles Include="Resources\**\*" />
</ItemGroup>

3. Build and use

// Direct access
var configBytes = EmbeddedResources.Config;
var configText = Encoding.UTF8.GetString(configBytes);

// Lookup by path
var logoBytes = EmbeddedResources.GetResource("Resources/logo.png");

// Get metadata
if (EmbeddedResources.Metadata.TryGetInfo("Config", out var info))
{
    Console.WriteLine($"Size: {info.Size} bytes");
    Console.WriteLine($"IsText: {info.IsText}");
}

// List all embedded files
foreach (var path in EmbeddedResources.GetAllPaths())
{
    Console.WriteLine(path);
}

Configuration

Text vs Binary Files

The generator auto-detects text files by extension (.txt, .json, .xml, .cs, etc.). You can override this:

[Embed("Resources/data.bin", IsText = true)]  // Force text
public static partial ReadOnlySpan<byte> Data { get; }

[Embed("Resources/file.txt", IsText = false)] // Force binary
public static partial ReadOnlySpan<byte> Binary { get; }

Dictionary Metadata

Add metadata to your resource dictionary:

[ResourceDictionary(Locale = "en-US", Description = "Application resources")]
public partial static class EmbeddedResources
{
    // ...
}

// Access metadata
var locale = EmbeddedResources.DictionaryMetadata.Locale;
var description = EmbeddedResources.DictionaryMetadata.Description;

Requirements

  • .NET 9.0 or later (for ReadOnlySpan<byte> properties)
  • C# 13.0 or later (for partial properties)

Diagnostics

Code Description
EMB001 File not found in AdditionalFiles
EMB002 Error reading file
EMB003 [ResourceDictionary] must be on partial static class
EMB004 [Embed] must be on partial static property
EMB005 [Embed] property must return ReadOnlySpan<byte>

Examples

See the sample project for a complete example.

License

MIT License - see LICENSE file for details.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on Embed.Generator:

Repository Stars
6over3/hako
An embeddable, lightweight, secure, high-performance JavaScript engine.
Version Downloads Last Updated
1.0.0 289 11/2/2025