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" />
<PackageReference Include="Embed.Generator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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
#tool nuget:?package=Embed.Generator&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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.
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.3.0)
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 4.3.0)
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 |