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
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | < 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
1 2 3 4 5 6 7 8 | // See https://aka.ms/new-console-template for more information using EmbedDemo; Console.WriteLine( "Hello, World!" ); var data= TestData.Countries; Console.WriteLine(data); |
1 2 3 4 | using Datacute.EmbeddedResourcePropertyGenerator; namespace EmbedDemo; [EmbeddedResourceProperties( ".txt" , "TestData" )] public static partial class TestData; |
1 2 3 4 5 | USA Germany France Romania Italy |
The code that is generated is
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | //------------------------------------------------------------------------------ // <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