Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# RSCG - 219 Examples of Roslyn Source Code Generators / 14 created by Microsoft /
# RSCG - 220 Examples of Roslyn Source Code Generators / 14 created by Microsoft /

## Latest Update : 2025-08-07 => 07 August 2025
## Latest Update : 2025-08-08 => 08 August 2025

If you want to see examples with code, please click ***[List V2](https://ignatandrei.github.io/RSCG_Examples/v2/docs/List-of-RSCG)***

Expand All @@ -20,8 +20,30 @@ If you want to be notified each time I add a new RSCG example , please click htt

## Content

Those are the 219 Roslyn Source Code Generators that I have tested you can see and download source code example.
Those are the 220 Roslyn Source Code Generators that I have tested you can see and download source code example.
( including 14 from Microsoft )
### 220. [NFH.FileEmbed](https://ignatandrei.github.io/RSCG_Examples/v2/docs/NFH.FileEmbed) , in the [FilesToCode](https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples#filestocode) category

Generated on : 2025-08-08 => 08 August 2025

<details>
<summary>Expand</summary>



Author: Natalie Howes

A source generator for embedding resource files directly into your assembly. Access them as a ReadOnlySpan<byte>, with no allocations or reflection needed.

Nuget: [https://www.nuget.org/packages/NFH.FileEmbed/](https://www.nuget.org/packages/NFH.FileEmbed/)


Link: [https://ignatandrei.github.io/RSCG_Examples/v2/docs/NFH.FileEmbed](https://ignatandrei.github.io/RSCG_Examples/v2/docs/NFH.FileEmbed)

Source: [https://github.com/SirCxyrtyx/FileEmbed](https://github.com/SirCxyrtyx/FileEmbed)

</details>

### 219. [SKPromptGenerator](https://ignatandrei.github.io/RSCG_Examples/v2/docs/SKPromptGenerator) , in the [AI](https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg-examples#ai) category

Generated on : 2025-08-07 => 07 August 2025
Expand Down
2 changes: 1 addition & 1 deletion later.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Just later

## Latest Update : 2025-08-07 => 07 August 2025
## Latest Update : 2025-08-08 => 08 August 2025



Expand Down
2 changes: 2 additions & 0 deletions v2/Generator/MultiGeneratorV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ public string[] SourceNoRSCG()
text = text.Replace("(docs/building.md)", $"({d.Generator!.Source}/docs/building.md)");
text = text.Replace("(./", $"({d.Generator!.Source}/");


text = text.Replace("<byte>", "Andrei<byte>");
Comment thread
ignatandrei marked this conversation as resolved.
return text;
Comment thread
ignatandrei marked this conversation as resolved.
}
;
Expand Down
1 change: 1 addition & 0 deletions v2/Generator/all.csv
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,4 @@ Nr,Key,Source,Category
217,EnumsEnhanced, https://github.com/snowberry-software/EnumsEnhanced,Enum
218,Nino, https://github.com/JasonXuDeveloper/Nino,Serializer
219,SKPromptGenerator, https://github.com/CharlieDigital/SKPromptGenerator,AI
220,NFH.FileEmbed, https://github.com/SirCxyrtyx/FileEmbed,FilesToCode
6 changes: 6 additions & 0 deletions v2/RSCGExamplesData/GeneratorDataRec.json
Original file line number Diff line number Diff line change
Expand Up @@ -1323,5 +1323,11 @@
"Category": 37,
"dtStart": "2025-08-07T00:00:00",
"show": true
},
{
"ID":"NFH.FileEmbed",
"Category": 8,
"dtStart": "2025-08-08T00:00:00",
"show": true
}
]
70 changes: 70 additions & 0 deletions v2/book/examples/NFH.FileEmbed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

<h1>RSCG nr 220 : NFH.FileEmbed</h1>

<h2>Info</h2>
Nuget : <a href="https://www.nuget.org/packages/NFH.FileEmbed/" target="_blank">https://www.nuget.org/packages/NFH.FileEmbed/</a>

<p>You can find more details at : <a href="https://github.com/SirCxyrtyx/FileEmbed" target="_blank"> https://github.com/SirCxyrtyx/FileEmbed</a></p>

<p>Author :Natalie Howes</p>

<p>Source: <a href="https://github.com/SirCxyrtyx/FileEmbed" target="_blank">https://github.com/SirCxyrtyx/FileEmbed</a> </p>
Comment thread
ignatandrei marked this conversation as resolved.

<h2>About</h2>

Generating embedded files

<h2>
How to use
</h2>
<h3>
Add reference to the <a href="https://www.nuget.org/packages/NFH.FileEmbed/" target="_blank">NFH.FileEmbed</a> in the csproj
</h3>
<img src="images/NFH.FileEmbed/EmbedDemo.csproj.png" width="580" height="580" />

<h3>This was for me the <b>starting</b> code</h3>

<br />
I have <b>coded</b> the file Program.cs
<br />
<img src="images/NFH.FileEmbed/csFiles/Program.cs.png" width="580" height="580" />
<hr />

<br />
I have <b>coded</b> the file TestData.cs
<br />
<img src="images/NFH.FileEmbed/csFiles/TestData.cs.png" width="580" height="580" />
<hr />

<br />
I have <b>coded</b> the file Countries.txt
<br />
<img src="images/NFH.FileEmbed/csFiles/Countries.txt.png" width="580" height="580" />
<hr />
<h3>And here are the <i>generated</i> files</h3>

<br />
The file <i>generated</i> is EMBEDDED__Countries.txt.g.cs
<br />
<img src="images/NFH.FileEmbed/generated/EMBEDDED__Countries.txt.g.cs.png" width="580" height="580" />

<br />
The file <i>generated</i> is FileEmbedAttribute.g.cs
<br />
<img src="images/NFH.FileEmbed/generated/FileEmbedAttribute.g.cs.png" width="580" height="580" />

<p>
You can download the code and this page as pdf from
<a target="_blank" href='https://ignatandrei.github.io/RSCG_Examples/v2/docs/NFH.FileEmbed'>
https://ignatandrei.github.io/RSCG_Examples/v2/docs/NFH.FileEmbed
</a>
</p>


<p>
You can see the whole list at
<a target="_blank" href='https://ignatandrei.github.io/RSCG_Examples/v2/docs/List-of-RSCG'>
https://ignatandrei.github.io/RSCG_Examples/v2/docs/List-of-RSCG
</a>
</p>

6 changes: 5 additions & 1 deletion v2/book/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</head>
<body>
<h1>
This is the list of 219 RSCG with examples =>
This is the list of 220 RSCG with examples =>
</h1>

<table >
Expand Down Expand Up @@ -902,6 +902,10 @@ <h1>
<td>219</td>
<td><a href="examples/SKPromptGenerator.html">SKPromptGenerator</a></td>
</tr>
<tr>
<td>220</td>
<td><a href="examples/NFH.FileEmbed.html">NFH.FileEmbed</a></td>
</tr>
</table>


Expand Down
1 change: 1 addition & 0 deletions v2/book/pandocHTML.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ input-files:
- examples/EnumsEnhanced.html
- examples/Nino.html
- examples/SKPromptGenerator.html
- examples/NFH.FileEmbed.html

# or you may use input-file: with a single value
# defaults:
Expand Down
22 changes: 22 additions & 0 deletions v2/rscg_examples/NFH.FileEmbed/description.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"generator":{
"name":"NFH.FileEmbed",
"nuget":[
"https://www.nuget.org/packages/NFH.FileEmbed/"
],
"link":"https://github.com/SirCxyrtyx/FileEmbed",
"author":"Natalie Howes",
"source":"https://github.com/SirCxyrtyx/FileEmbed"
},
"data":{
"goodFor":["Generating embedded files "],
"csprojDemo":"EmbedDemo.csproj",
"csFiles":["Program.cs","TestData.cs","Countries.txt"],
"excludeDirectoryGenerated":[""],
"includeAdditionalFiles":[""]
},
"links":{
"blog":"",
"video":""
}
}
1 change: 1 addition & 0 deletions v2/rscg_examples/NFH.FileEmbed/nuget.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A source generator for embedding resource files directly into your assembly. Access them as a ReadOnlySpan<byte>, with no allocations or reflection needed.
76 changes: 76 additions & 0 deletions v2/rscg_examples/NFH.FileEmbed/readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# C# FileEmbed

A source generator for embedding resource files directly into your assembly. Access them as a `ReadOnlySpan<byte>`, with no allocations or reflection needed.

Add the package to your application using

```
dotnet add package NFH.FileEmbed
```

You will probably wish to add `PrivateAssets="all"` to the `PackageReference` for this package in your csproj, to prevent this becoming a dependency (as it's only needed at compile time).

## Example Usage
```csharp
//attribute is in this namespace
using FileEmbed;

namespace EmbedExample;

//partial methods must be in partial types
public static partial class Program
{
//Place the attribute on a static partial method that returns a ReadOnlySpan<byte>
[FileEmbed(@"Capture.PNG")]
public static partial ReadOnlySpan<byte> Bytes();


//works in any type that can contain a static method
private partial record struct MyStruct
{
//Path is relative to your project directory (specifically, the ProjectDir MSBuild property)
[FileEmbed(@"Resources\Capture.7z")]
internal static partial ReadOnlySpan<byte> StructBytes();
}

public partial interface IExampleInterface
{
//two optional arguments, Offset and Length, allow you to embed a slice of the file
[FileEmbed(@"Resources\Capture.7z", 4, 8)]
internal static partial ReadOnlySpan<byte> InterfaceBytes();
}

public static void Main()
{
Console.WriteLine($"{Bytes().Length} bytes");
Console.WriteLine($"{MyStruct.StructBytes().Length} bytes");
Console.WriteLine($"{IExampleInterface.InterfaceBytes().Length} bytes");
}
}
```
Autogenerated code for `IExampleInterface.InterfaceBytes`:
```csharp
namespace EmbedExample
{
partial class Program
{
partial interface IExampleInterface
{
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("FileEmbed", "0.1.0.0")]
internal static partial global::System.ReadOnlySpan<byte> InterfaceBytes() => new byte[] { 39,28,0,4,115,228,9,158, };
}
}
}
```

## Limitations
This doesn't work so well for input files larger than 1MB unfortunately. The source generator itself can handle large files just fine, but, in my experience, Visual Studio chokes on the generated output, using huge amounts of memory after the files are generated. I have not yet investigated why. To protect users of this source generator from accidently locking up VS, it imposes a 1MB limit on input files. This can be overriden if you wish to try using this with larger files. To do so, put this in your csproj file:
```xml
<!-- To set FileEmbed_MaxEmbedSize, you must first make it visible to the compiler -->
<ItemGroup>
<CompilerVisibleProperty Include="FileEmbed_MaxEmbedSize" />
</ItemGroup>
<PropertyGroup>
<FileEmbed_MaxEmbedSize>SIZE_IN_BYTES_GOES_HERE</FileEmbed_MaxEmbedSize>
</PropertyGroup>
```
48 changes: 48 additions & 0 deletions v2/rscg_examples/NFH.FileEmbed/src/.tours/NFH.FileEmbed.tour
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

{
"$schema": "https://aka.ms/codetour-schema",
"title": "NFH.FileEmbed",
"steps":
[
{
"file": "EmbedDemo/EmbedDemo.csproj",
"description": "First, we add Nuget [NFH.FileEmbed](https://www.nuget.org/packages/NFH.FileEmbed/) in csproj ",
"pattern": "NFH.FileEmbed"
}

,{
"file": "EmbedDemo/TestData/Countries.txt",
"description": "File Countries.txt ",
"pattern": "this is the code"
}

,{
"file": "EmbedDemo/TestData.cs",
"description": "File TestData.cs ",
"pattern": "this is the code"
}

,{
"file": "EmbedDemo/Program.cs",
"description": "File Program.cs \r\n>> dotnet run --project EmbedDemo/EmbedDemo.csproj ",
"pattern": "this is the code"
}


,{
"file": "EmbedDemo/obj/GX/FileEmbed/FileEmbed.FileEmbedGenerator/FileEmbedAttribute.g.cs",
"description": "Generated File 2 from 2 : FileEmbedAttribute.g.cs ",
"line": 1
}

,{
"file": "EmbedDemo/obj/GX/FileEmbed/FileEmbed.FileEmbedGenerator/EMBEDDED__Countries.txt.g.cs",
"description": "Generated File 1 from 2 : EMBEDDED__Countries.txt.g.cs ",
"line": 1
}

],

"ref": "main"

}
22 changes: 22 additions & 0 deletions v2/rscg_examples/NFH.FileEmbed/src/EmbedDemo.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35424.110 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EmbedDemo", "EmbedDemo\EmbedDemo.csproj", "{71D83A8E-C979-4D40-91BD-4A6EB7771B4F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{71D83A8E-C979-4D40-91BD-4A6EB7771B4F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{71D83A8E-C979-4D40-91BD-4A6EB7771B4F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{71D83A8E-C979-4D40-91BD-4A6EB7771B4F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{71D83A8E-C979-4D40-91BD-4A6EB7771B4F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
33 changes: 33 additions & 0 deletions v2/rscg_examples/NFH.FileEmbed/src/EmbedDemo/EmbedDemo.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>


<ItemGroup>
<EmbeddedResource Include="TestData\Countries.txt" />
</ItemGroup>


<ItemGroup>
<PackageReference Include="NFH.FileEmbed" Version="0.1.0-beta" />
</ItemGroup>

<PropertyGroup>
<AdditionalFileItemNames>$(AdditionalFileItemNames);EmbeddedResource</AdditionalFileItemNames>
</PropertyGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<!--<ItemGroup>
<CompilerVisibleProperty Include="FileEmbed_MaxEmbedSize" />
</ItemGroup>
<PropertyGroup>
<FileEmbed_MaxEmbedSize>SIZE_IN_BYTES_GOES_HERE</FileEmbed_MaxEmbedSize>
</PropertyGroup>-->
</Project>
9 changes: 9 additions & 0 deletions v2/rscg_examples/NFH.FileEmbed/src/EmbedDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// See https://aka.ms/new-console-template for more information
using EmbedDemo;
using System.Text;

Console.WriteLine("Hello, World!");

var data= TestData.Countries();

Console.WriteLine(Encoding.UTF8.GetString(data) );
Loading