Skip to main content

Chorn.EmbeddedResourceAccessGenerator by Christoph Hornung

Nuget / site data

Nuget GitHub last commit GitHub Repo stars

Details

Info

info

Name: Chorn.EmbeddedResourceAccessGenerator

Generates strongly typed access methods for embedded resources.

Author: Christoph Hornung

NuGet: https://www.nuget.org/packages/Chorn.EmbeddedResourceAccessGenerator/

You can find more details at https://github.com/ChristophHornung/EmbeddedResourceGenerator

Source : https://github.com/ChristophHornung/EmbeddedResourceGenerator

Original Readme

note

EmbeddedResourceAccessGenerator

NuGet version (Chorn.EmbeddedResourceAccessGenerator)

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

Usage

Get the nuget package here.

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:
    // 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:
    // 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:
    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.

About

note

Embedded Resources to C# Code

How to use

Example ( source csproj, source files )

This is the CSharp Project that references Chorn.EmbeddedResourceAccessGenerator

<Project Sdk="Microsoft.NET.Sdk">

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

<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
<ItemGroup>
<EmbeddedResource Include="sql/*.sql" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Chorn.EmbeddedResourceAccessGenerator" Version="1.1.5" OutputItemType="Analyzer" >
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>


</Project>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

#nullable enable
namespace EmbedDemo;
using System;
using System.Collections;
using System.IO;
using System.Reflection;

/// <summary>
/// Auto-generated class to access all embedded resources in an assembly.
/// </summary>
public static partial class EmbeddedResources
{
/// <summary>
/// Gets the embedded resource 'sql.createDB.sql' as a stream.
/// </summary>
/// <returns>The stream to access the embedded resource.</returns>
public static Stream sql_createDB_sql_Stream
{
get {
Assembly assembly = typeof(EmbeddedResources).Assembly;
string resource = "EmbedDemo.sql.createDB.sql";
return assembly.GetManifestResourceStream(resource)!;
}
}

/// <summary>
/// Gets the embedded resource 'sql.createDB.sql' as a stream-reader.
/// </summary>
/// <returns>The stream-reader to access the embedded resource.</returns>
public static StreamReader sql_createDB_sql_Reader
{
get
{
Assembly assembly = typeof(EmbeddedResources).Assembly;
string resource = "EmbedDemo.sql.createDB.sql";
return new StreamReader(assembly.GetManifestResourceStream(resource)!);
}
}

/// <summary>
/// Gets the embedded resource's stream.
/// </summary>
/// <param name="resource">The embedded resource to retrieve the stream for.</param>
/// <returns>The stream to access the embedded resource.</returns>
public static Stream GetStream(this EmbeddedResource resource)
{
Assembly assembly = typeof(EmbeddedResources).Assembly;
return assembly.GetManifestResourceStream(GetResourceName(resource))!;
}

/// <summary>
/// Gets the embedded resource's stream-reader.
/// </summary>
/// <param name="resource">The embedded resource to retrieve the stream-reader for.</param>
/// <returns>The stream-reader to access the embedded resource.</returns>
public static StreamReader GetReader(this EmbeddedResource resource)
{
Assembly assembly = typeof(EmbeddedResources).Assembly;
return new StreamReader(assembly.GetManifestResourceStream(GetResourceName(resource))!);
}

/// <summary>
/// Gets the embedded resource's name in the format required by <c>GetManifestResourceStream</c>.
/// </summary>
/// <param name="resource">The embedded resource to retrieve the name for.</param>
/// <returns>The name to access the embedded resource.</returns>
public static string GetResourceName(this EmbeddedResource resource)
{
return resource switch
{
EmbeddedResource.sql_createDB_sql => "EmbedDemo.sql.createDB.sql",
_ => throw new InvalidOperationException(),
};
}

/// <summary>
/// Gets the embedded resource's stream.
/// </summary>
/// <param name="resource">The embedded resource to retrieve the stream for.</param>
/// <returns>The stream to access the embedded resource.</returns>
public static Stream GetStream(this EmbeddedResourcesql resource)
{
Assembly assembly = typeof(EmbeddedResources).Assembly;
return assembly.GetManifestResourceStream(GetResourceName(resource))!;
}

/// <summary>
/// Gets the embedded resource's stream-reader.
/// </summary>
/// <param name="resource">The embedded resource to retrieve the stream-reader for.</param>
/// <returns>The stream-reader to access the embedded resource.</returns>
public static StreamReader GetReader(this EmbeddedResourcesql resource)
{
Assembly assembly = typeof(EmbeddedResources).Assembly;
return new StreamReader(assembly.GetManifestResourceStream(GetResourceName(resource))!);
}

/// <summary>
/// Gets the embedded resource's name in the format required by <c>GetManifestResourceStream</c>.
/// </summary>
/// <param name="resource">The embedded resource to retrieve the name for.</param>
/// <returns>The name to access the embedded resource.</returns>
public static string GetResourceName(this EmbeddedResourcesql resource)
{
return resource switch
{
EmbeddedResourcesql.createDB_sql => "EmbedDemo.sql.createDB.sql",
_ => throw new InvalidOperationException(),
};
}
}

/// <summary>
/// Auto-generated enumeration for all embedded resources in the assembly.
/// </summary>
public enum EmbeddedResource
{
/// <summary>
/// Represents the embedded resource 'sql.createDB.sql'.
/// </summary>
sql_createDB_sql,
}

/// <summary>
/// Auto-generated enumeration for all embedded resources in 'sql'.
/// </summary>
public enum EmbeddedResourcesql
{
/// <summary>
/// Represents the embedded resource 'createDB.sql' in sql.
/// </summary>
createDB_sql,
}
#nullable restore

Usefull

Download Example (.NET C# )

Share Chorn.EmbeddedResourceAccessGenerator

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Chorn.EmbeddedResourceAccessGenerator

In the same category (FilesToCode) - 13 other generators

corecraft

Datacute.EmbeddedResourcePropertyGenerator

DotnetYang

EmbedResourceCSharp

LingoGen

NotNotAppSettings

Podimo.ConstEmbed

ResXGenerator

RSCG_JSON2Class

RSCG_Utils

ThisAssembly_Resources

ThisAssembly.Strings

Weave