Raffinert.EmbeddedResourceAccessGenerator 1.0.4

dotnet add package Raffinert.EmbeddedResourceAccessGenerator --version 1.0.4                
NuGet\Install-Package Raffinert.EmbeddedResourceAccessGenerator -Version 1.0.4                
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="Raffinert.EmbeddedResourceAccessGenerator" Version="1.0.4">
  <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.
paket add Raffinert.EmbeddedResourceAccessGenerator --version 1.0.4                
#r "nuget: Raffinert.EmbeddedResourceAccessGenerator, 1.0.4"                
#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.
// Install Raffinert.EmbeddedResourceAccessGenerator as a Cake Addin
#addin nuget:?package=Raffinert.EmbeddedResourceAccessGenerator&version=1.0.4

// Install Raffinert.EmbeddedResourceAccessGenerator as a Cake Tool
#tool nuget:?package=Raffinert.EmbeddedResourceAccessGenerator&version=1.0.4                

StandWithUkraine

A refined version of the original EmbeddedResourceAccessGenerator by ChristophHornung

Important: The original EmbeddedResourceAccessGenerator is incompatible with this generator and must be removed before adding this generator.

Improvements in the EmbeddedResourceAccessGenerator:

  • Fixed a bug when the generator generates code for all AdditionalFiles, not EmbeddedResources only. This means that the original EmbeddedResourceAccessGenerator is incompatible with all three generators and must be removed before adding those generators.
  • Removed properties from the EmbeddedResources class, because it's an anti-pattern to create disposable resources on every get property access.
  • The GetReader method now creates the StreamReader with the option leaveOpen: false. It disposes the stream together with the StreamReader.
  • Added synchronous and asynchronous extension methods ReadAllText, ReadAllTextAsync, ReadAllBytes, ReadAllBytesAsync.

EmbeddedResourceAccessGenerator

NuGet version (Raffinert.EmbeddedResourceAccessGenerator)

The Raffinert.EmbeddedResourceAccessGenerator is a code generator to allow easy access to all embedded resources.

Usage

Get the nuget package here.

After referencing the Raffinert.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:
    // Via the generated extension methods on the enum
    using Stream s = EmbeddedResource.TestAsset_Test_txt.GetStream();
    using StreamReader sr = EmbeddedResource.TestAsset_Test_txt.GetReader();
    string text = EmbeddedResource.TestAsset_Test_txt.ReadAllText();
    string textAsync = await EmbeddedResource.TestAsset_Test_txt.ReadAllTextAsync(CancellationToken.None);
    byte[] bytes = EmbeddedResource.TestAsset_Test_txt.ReadAllBytes();
    byte[] bytesAsync = await EmbeddedResource.TestAsset_Test_txt.ReadAllBytesAsync(CancellationToken.None);

  • Via enum access through the EmbeddedResource[FolderName] enum:
    // Via the generated extension methods on the enum
    using Stream s = EmbeddedResource_TestAsset.Test_txt.GetStream();
    using StreamReader sr = EmbeddedResource_TestAsset.Test_txt.GetReader();
    string text = EmbeddedResource_TestAsset.Test_txt.ReadAllText();
    string textAsync = await EmbeddedResource_TestAsset.Test_txt.ReadAllTextAsync(CancellationToken.None);
    byte[] bytes = EmbeddedResource_TestAsset.Test_txt.ReadAllBytes();
    byte[] bytesAsync = await EmbeddedResource_TestAsset.Test_txt.ReadAllBytesAsync(CancellationToken.None);

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.

See also:

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.4 69 10/22/2024
1.0.3 61 10/22/2024
1.0.2 79 10/14/2024
1.0.1 85 10/8/2024
1.0.0 85 10/7/2024

v 1.0.4 - Fix missing using System.Threading directive.
v 1.0.3 - Fix missing using System.Threading.Tasks directive.
v 1.0.2 - Separate readmes between the projects and update them.
v 1.0.1 - Added ReadAllBytes and ReadAllBytesAsync methods
v 1.0.0
- Removed EmbeddedResources.{{identifierName}}_Stream and EmbeddedResources.StreamReader {{identifierName}}_Reader properties
- Added ReadAllText and ReadAllTextAsync methods
- No longer generating access for all AdditionalFiles as EmbeddedResources and thereforeit can be used together with ContentItemAccessGenerator and NoneItemAccessGenerator without conflicts.
Precedor's versions - see https://github.com/ChristophHornung/EmbeddedResourceGenerator/blob/main/EmbeddedResourceAccessGenerator/EmbeddedResourceAccessGenerator.csproj