RSCG – Chorn.EmbeddedResourceAccessGenerator

RSCG – Chorn.EmbeddedResourceAccessGenerator
 
 

name Chorn.EmbeddedResourceAccessGenerator
nuget https://www.nuget.org/packages/Chorn.EmbeddedResourceAccessGenerator/
link https://github.com/ChristophHornung/EmbeddedResourceGenerator
author Christoph Hornung

Embedded Resources to C# Code

 

This is how you can use Chorn.EmbeddedResourceAccessGenerator .

The code that you start with is


<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>


The code that you will use is


using EmbedDemo;
using StreamReader sr = EmbeddedResource.sql_createDB_sql.GetReader();
var data=sr.ReadToEnd();
Console.WriteLine(data);



create database Andrei;
GO;
use Andrei;
GO;


 

The code that is generated is

#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

Code and pdf at

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