Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ChristophHornung/EmbeddedResourceGenerator
A C# source generator to automatically generate access methods for embedded resources.
https://github.com/ChristophHornung/EmbeddedResourceGenerator
csharp csharp-sourcegenerator
Last synced: 3 days ago
JSON representation
A C# source generator to automatically generate access methods for embedded resources.
- Host: GitHub
- URL: https://github.com/ChristophHornung/EmbeddedResourceGenerator
- Owner: ChristophHornung
- License: mit
- Created: 2023-01-21T09:45:56.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2023-05-01T09:19:51.000Z (over 1 year ago)
- Last Synced: 2024-10-01T21:45:39.335Z (about 1 month ago)
- Topics: csharp, csharp-sourcegenerator
- Language: C#
- Homepage:
- Size: 161 KB
- Stars: 8
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- RSCG_Examples - https://github.com/ChristophHornung/EmbeddedResourceGenerator
README
# EmbeddedResourceAccessGenerator
[![NuGet version (Chorn.EmbeddedResourceAccessGenerator)](https://img.shields.io/nuget/v/Chorn.EmbeddedResourceAccessGenerator.svg?style=flat-square)](https://www.nuget.org/packages/Chorn.EmbeddedResourceAccessGenerator/)The EmbeddedResourceAccessGenerator is a code generator to allow easy access to all
embedded resources.## Usage
Get the nuget package [here](https://www.nuget.org/packages/Chorn.EmbeddedResourceAccessGenerator).After referencing the `Chorn.EmbeddedResourceAccessGenerator` nuget the code generation will
automatically create a class `EmbeddedResources` in the root namespace of the project.Together with the generated `EmbeddedResource` enumeration there are several options to access
embedded resources:E.g. for a `Test.txt` embedded resource in the `TestAsset` folder:
- Via enum access through the `EmbeddedResource` enum:
```csharp
// Via the generated extension methods on the enum
using Stream s = EmbeddedResource.TestAsset_Test_txt.GetStream();
using StreamReader sr = EmbeddedResource.TestAsset_Test_txt.GetReader();
```- Via enum access through the `EmbeddedResource[FolderName]` enum:
```csharp
// Via the generated extension methods on the enum
using Stream s = EmbeddedResourceTestAsset.Test_txt.GetStream();
using StreamReader sr = EmbeddedResourceTestAsset.Test_txt.GetReader();
```- Via direct static acccess on `EmbeddedResources`:
```csharp
using StreamReader sr = EmbeddedResources.TestAsset_Test_txt_Reader;
Console.WriteLine(sr.ReadToEnd());// Or via access through the Stream
using Stream s = EmbeddedResources.TestAsset_Test_txt_Stream;
// ...
```## Motivation
Instead of using magic strings in the resource access code that may point to non-existant
resources this generator guarantees resources to exist and code to not compile when they are
removed.Grouping the resources via their path adds path specific enums, e.g. to easily write tests
for all embedded resource in a subfolder.Also it saves quite a bit of typing effort.