Skip to content

A C# source generator to automatically generate access methods for embedded resources.

License

Notifications You must be signed in to change notification settings

ChristophHornung/EmbeddedResourceGenerator

Repository files navigation

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

A C# source generator to automatically generate access methods for embedded resources.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages