RSCG – Datacute.EmbeddedResourcePropertyGenerator
RSCG – Datacute.EmbeddedResourcePropertyGenerator
name | Datacute.EmbeddedResourcePropertyGenerator |
nuget | https://www.nuget.org/packages/Datacute.EmbeddedResourcePropertyGenerator/ |
link | https://github.com/datacute/EmbeddedResourcePropertyGenerator/ |
author | Stephen Denne |
Generating class to access easy the embedded resource
This is how you can use Datacute.EmbeddedResourcePropertyGenerator .
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> <ItemGroup> <EmbeddedResource Include="TestData\Countries.txt" /> </ItemGroup> <ItemGroup> <PackageReference Include="Datacute.EmbeddedResourcePropertyGenerator" Version="1.0.0" > </PackageReference> </ItemGroup> <PropertyGroup> <AdditionalFileItemNames>$(AdditionalFileItemNames);EmbeddedResource</AdditionalFileItemNames> </PropertyGroup> <PropertyGroup> <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath> </PropertyGroup> </Project>
The code that you will use is
// See https://aka.ms/new-console-template for more information using EmbedDemo; Console.WriteLine("Hello, World!"); var data= TestData.Countries; Console.WriteLine(data);
using Datacute.EmbeddedResourcePropertyGenerator; namespace EmbedDemo; [EmbeddedResourceProperties(".txt", "TestData")] public static partial class TestData;
USA Germany France Romania Italy
The code that is generated is
//------------------------------------------------------------------------------ // <auto-generated> // This code was generated by the Datacute.EmbeddedResourcePropertyGenerator. // </auto-generated> //------------------------------------------------------------------------------ #nullable enable namespace EmbedDemo; /// <summary> /// This class's properties are generated from project files meeting the criteria: /// <list type="bullet"> /// <item> /// <description>they are both an <c>EmbeddedResource</c> and an <c>AdditionalFile</c></description> /// </item> /// <item> /// <description>they are in the project folder <c>TestData</c></description> /// </item> /// <item> /// <description>they have the extension <c>.txt</c></description> /// </item> /// </list> /// </summary> public static partial class TestData { private static class EmbeddedResource { public static string Read(string resourceName) { var assembly = typeof(TestData).Assembly; using var stream = assembly.GetManifestResourceStream(resourceName)!; using var streamReader = new global::System.IO.StreamReader(stream, global::System.Text.Encoding.UTF8); var resourceText = streamReader.ReadToEnd(); return resourceText; } public static class BackingField { public static string? Countries; } public static class ResourceName { public const string Countries = "EmbedDemo.TestData.Countries.txt"; } } static partial void ReadEmbeddedResourceValue(ref string? backingField, string resourceName, string propertyName); static partial void AlterEmbeddedResourceReturnValue(ref string value, string resourceName, string propertyName); /// <summary>Text value of the Embedded Resource: Countries.txt</summary> /// <value> /// <code> /// USA /// Germany /// France /// Romania /// Italy /// /// </code> /// </value> /// <remarks> /// The value is read from the embedded resource on first access. /// </remarks> public static string Countries { get { ReadEmbeddedResourceValue(ref EmbeddedResource.BackingField.Countries, EmbeddedResource.ResourceName.Countries, "Countries"); var value = EmbeddedResource.BackingField.Countries ??= EmbeddedResource.Read(EmbeddedResource.ResourceName.Countries); AlterEmbeddedResourceReturnValue(ref value, EmbeddedResource.ResourceName.Countries, "Countries"); return value; } } }
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/Datacute.EmbeddedResourcePropertyGenerator
Leave a Reply