Are you one of the C# devs that doesn't know by heart how to get hold of an embedded resource? Light.EmbeddedResources makes it easy for you via extension methods:
// Either get it as a stream
using var stream = this.GetEmbeddedStream("EmailTemplate.html");
// ...or as a string
string value = this.GetEmbeddedResource("EmailTemplate.html");If you work in a static method, you can use types:
using var stream = typeof(MyCurrentType).GetEmbeddedStream("EmailTemplate.html");
string value = typeof(MyCurrentType).GetEmbeddedResource("EmailTemplate.html");Just make sure that the instance / the type you call GetEmbeddedStream or GetEmbeddedResource on is in the same assembly and namespace as the resource you want to retrieve.
You need to prepare a context, e.g. when you dynamically create PDF files out of prepared templates and the user data? You can easily copy your embedded resources to the file system:
this.CopyEmbeddedStreamToFile("EmailTemplate.html", Path.Combine(targetDirectory, "template.html"));Of course, there is also an overload that works with types.
I tend to have my embedded resource files directly next to the types that use them, so that I can simply call the extension methods on the target instances. My folder structure could e.g. look like this:
- Contracts
|- ContractEmailSender.cs // a class that uses embedded resources
|- Contract.html // embedded email template that will be converted to a PDF file
|- company-logo.png // logo that will be embedded in the html file via the HTML-to-PDF engine
If you prefer to have all your embedded resources in one folder or in different assemblies, then create a type next to them to allow easy access:
- EmbeddedResources
|- Resource1.json
|- Resource2.xml
|- Resources.cs
- OtherNamespace
|- SomeService.cs // This service needs to access EmbeddedResources in another namespace / assembly
In this case, Resources and the client call site could be implemented like this:
public readonly struct Resources
{
public static readonly Instance = new Resources();
}
public class SomeService
{
public void SomeFunction()
{
using var resource1Stream = Resources.Instance.GetEmbeddedStream("Resource1.json");
}
}Light.EmbeddedResources is compiled for
- .NET Standard 2.0
- .NET 5.0
and runs on all platforms that support these (like full .NET Framework, Mono, Xamarin, Unity, UWP, etc.).
Light.EmbeddedResources is available as a NuGet package:
- via csproj:
<PackageReference Include="Light.EmbeddedResources" Version="1.1.0" /> - via .NET CLI:
dotnet add package Light.EmbeddedResources --version 1.1.0 - via VS Package Manager:
Install-Package Light.EmbeddedResources -Version 1.1.0