Show / Hide Table of Contents

Class ResourceLoader

This class does all the magic. Given a type, or an instance of a type containing fields or properties decorated with the EmbeddedResourceAttribute it loads up all the fields and properties with the indicated embedded resources.

Resources are only loaded into members that have not previously been initialized (i.e are null). This also has the beneficial side effect of ensuring that static members are only ever loaded once, which is when the first instance of the containing type is presented.

Inheritance
Object
ResourceLoader
Inherited Members
Object.Equals(Object)
Object.Equals(Object, Object)
Object.GetHashCode()
Object.GetType()
Object.MemberwiseClone()
Object.ReferenceEquals(Object, Object)
Object.ToString()
Namespace: Firefly.EmbeddedResourceLoader
Assembly: Firefly.EmbeddedResourceLoader.dll
Syntax
public static class ResourceLoader
Remarks

The load process works by looking for an appropriate way of initializing the member in the following order...

  1. If the member is a string, then read the resource as a string and assign it directly.
  2. Look for a constructor of the member's type that takes a single argument of Stream. Good for many binary types like images and so forth.
  3. Look for a public static or instance Load method taking a single argument of type Stream or StreamReader, e.g. XDocument or YamlStream
  4. Look for a public static Parse method taking a single argument of type String, e.g. JObject.
  5. Search registered plugins for a plugin that supports the Type of the field or property.
  6. Throw an exception.

Methods

| Improve this Doc View Source

GetFileResource(String)

Gets a resource's content as a TempFile. The caller is responsible for disposal.

The file extension of the temporary file will be .tmp and the resource is assumed to be in the assembly containing the code that calls this method.

Declaration
public static TempFile GetFileResource(string partialResourcePath)
Parameters
Type Name Description
String partialResourcePath

The partial resource path.

Returns
Type Description
TempFile

A new TempFile containing the resource content.

| Improve this Doc View Source

GetFileResource(String, Boolean)

Gets a resource's content as a TempFile. The caller is responsible for disposal.

The resource is assumed to be in the assembly containing the code that calls this method.

Declaration
public static TempFile GetFileResource(string partialResourcePath, bool preserveExtension)
Parameters
Type Name Description
String partialResourcePath

The partial resource path.

Boolean preserveExtension

if set to true preserve the file extension of the resource.

Returns
Type Description
TempFile

A new TempFile containing the resource content.

| Improve this Doc View Source

GetFileResource(String, Boolean, Assembly)

Gets a resource's content as a TempFile. The caller is responsible for disposal.

Declaration
public static TempFile GetFileResource(string partialResourcePath, bool preserveExtension, Assembly containingAssembly)
Parameters
Type Name Description
String partialResourcePath

The partial resource path.

Boolean preserveExtension

if set to true preserve the file extension of the resource.

Assembly containingAssembly

The assembly containing the requested resource.

Returns
Type Description
TempFile

A new TempFile containing the resource content.

| Improve this Doc View Source

GetResourceStream(String, Assembly)

Gets the manifest resource stream for the given resource.

Declaration
public static Stream GetResourceStream(string partialResourcePath, Assembly containingAssembly)
Parameters
Type Name Description
String partialResourcePath

Partial (trailing) path to embedded resource.

Assembly containingAssembly

Assembly that contains the resource to load. If null then the assembly containing the code that calls this method is searched.

Returns
Type Description
Stream

A stream from which to load the resource data.

Exceptions
Type Condition
ResourceLoaderInvalidPathException

Resource path was incorrect, or you did not set the build action to Embedded Resource

| Improve this Doc View Source

GetStringResource(Stream)

Reads a resource stream into a string.

Declaration
public static object GetStringResource(Stream resourceStream)
Parameters
Type Name Description
Stream resourceStream

The resource stream returned by GetResourceStream(String, Assembly).

Returns
Type Description
Object

String containing resource data.

| Improve this Doc View Source

GetStringResource(String)

Reads a resource from the assembly containing the method that calls this overload into a string.

Declaration
public static string GetStringResource(string partialResourcePath)
Parameters
Type Name Description
String partialResourcePath

The partial resource path.

Returns
Type Description
String

String containing resource data.

| Improve this Doc View Source

GetStringResource(String, Assembly)

Reads a resource in the specified assembly into a string.

Declaration
public static string GetStringResource(string partialResourcePath, Assembly containingAssembly)
Parameters
Type Name Description
String partialResourcePath

The partial resource path.

Assembly containingAssembly

The assembly containing the resource.

Returns
Type Description
String

String containing resource data.

| Improve this Doc View Source

LoadResources(Object)

This method examines the object passed to it for members decorated with the EmbeddedResourceAttribute attribute and loads any indicated resources.

Declaration
public static void LoadResources(object typeOrInstance)
Parameters
Type Name Description
Object typeOrInstance

Either a Type, or an instance of some object.

Remarks

In the case where typeOrInstance is a Type, the type will be examined only for static members decorated with the attribute. This is useful for static classes, or instance classes for which you only want to initialize the static members.

If typeOrInstance is an instance of an object, decorated static members will be initialized along with all decorated instance members.

In all cases members are only initialized with embedded resource data if the member is still null at the time this method is called.

Note that value types are always initialized with their default value if no actual initialization to some other value is present and as such are initialized, therefore the EmbeddedResourceAttribute is simply ignored.

Examples
using System.Xml.Linq;
using Firefly.EmbeddedResourceLoader;

namespace TestNamespace
{
    static class StaticClass
    {
        [EmbeddedResource("TestNamespace.Resources.TestResource2.xml")]
        public static XDocument xdoc = null;
    }

    class Program
    {
        static void Main(string[] args)
        {
            // Initialise members of a static class
            ResourceLoader.LoadResources(typeof(StaticClass));
        }
    }
}
using System.Xml.Linq;
using Firefly.EmbeddedResourceLoader;

namespace TestNamespace
{
    class TestClass
    {
        [EmbeddedResource("TestNamespace.Resources.TestResource1.txt")]
        public string testResource1;

        [EmbeddedResource("TestNamespace.Resources.TestResource2.xml")]
        private XDocument testResource2;

        public TestClass()
        {
            // Initialise instance and any uninitialized static members.
            ResourceLoader.LoadResources(this);
        }
    }
}
using System.Xml.Linq;
using Firefly.EmbeddedResourceLoader;

namespace TestNamespace
{
    class TestClass
    {
        // Partial resource paths must be unambiguous
        [EmbeddedResource("TestResource1.txt")]
        public string testResource1;

        [EmbeddedResource("TestResource2.xml", "MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
        private XDocument testResource2;

        public TestClass()
        {
            // Initialise instance and any uninitialized static members.
            ResourceLoader.LoadResources(this);
        }
    }
}
Exceptions
Type Condition
ResourceLoaderInvalidTypeException

The type of the attributed member does not support resource loading.

ResourceLoaderReadOnlyException

Property does not have a set accessor.

ResourceLoaderInvalidPathException

The resource could not be found. Either the path is incorrect, or the resource wasn't set to Embedded Resource in the Solution Explorer properties.

ResourceLoaderAmbiguousPathException

The partial resource path specified matches more than one resource in the assembly manifest.

| Improve this Doc View Source

RegisterPlugin(IEnumerable<IResourceLoaderPlugin>)

Register a type or types from a plug-in

Declaration
public static void RegisterPlugin(IEnumerable<IResourceLoaderPlugin> typesToRegister)
Parameters
Type Name Description
IEnumerable<IResourceLoaderPlugin> typesToRegister

The types to register.

Exceptions
Type Condition
ArgumentNullException

typesToRegister is null

  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX