Simplifies reading embedded resources from an Assembly.
See Milestones for release notes.
Static resource names are checked at compile time. Use Resource.AsString
and Resource.AsStream
.
Runtime resource names are not check but can still make use of the helper code. Use Resource.AsStringUnChecked
and Resource.AsStreamUnChecked
.
This is an add-in for Fody
It is expected that all developers using Fody become a Patron on OpenCollective. See Licensing/Patron FAQ for more information.
See also Fody usage.
Install the Resourcer.Fody NuGet package and update the Fody NuGet package:
PM> Install-Package Fody
PM> Install-Package Resourcer.Fody
The Install-Package Fody
is required since NuGet always defaults to the oldest, and most buggy, version of any dependency.
Add <Resourcer/>
to FodyWeavers.xml
<Weavers>
<Resourcer/>
</Weavers>
Assuming you have an embedded resource at the root of your assembly named ResourceName
and your assembly is named AssemblyName
.
class Sample
{
void ReadResourceAsString()
{
var stringValue = Resource.AsString("ResourceName");
}
void ReadResourceAsStream()
{
var streamValue = Resource.AsStream("ResourceName");
}
}
class Sample
{
void ReadResourceAsString()
{
string stringValue;
var assembly = Assembly.GetExecutingAssembly();
using (var stream = assembly.GetManifestResourceStream("AssemblyName.ResourceName"))
using (var streamReader = new StreamReader(stream))
{
stringValue = streamReader.ReadToEnd();
}
}
void ReadResourceAsStream()
{
var assembly = Assembly.GetExecutingAssembly();
var streamValue = assembly.GetManifestResourceStream("AssemblyName.ResourceName");
}
}
Box designed by Mourad Mokrane from The Noun Project.