Skip to content
/ Resourcer Public

Simplifies reading embedded resources from an Assembly.

License

Notifications You must be signed in to change notification settings

Fody/Resourcer

Repository files navigation

Resourcer.Fody

Chat on Gitter NuGet Status

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.

Usage

See also Fody usage.

NuGet installation

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 to FodyWeavers.xml

Add <Resourcer/> to FodyWeavers.xml

<Weavers>
  <Resourcer/>
</Weavers>

What it does

Assuming you have an embedded resource at the root of your assembly named ResourceName and your assembly is named AssemblyName.

Your Code

class Sample
{
    void ReadResourceAsString()
    {
        var stringValue = Resource.AsString("ResourceName");
    }

    void ReadResourceAsStream()
    {
        var streamValue = Resource.AsStream("ResourceName");
    }
}

What gets compiled

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");
    }
}

Icon

Box designed by Mourad Mokrane from The Noun Project.