Class EmbeddedResourceReader
Reads content of an embedded resource which name is provided in the way of relative path within provided assembly (project).
public class EmbeddedResourceReader
- Inheritance
-
objectEmbeddedResourceReader
- Derived
-
EmbeddedResourceReader<TAnySourceAssemblyType>
- Extension Methods
Remarks
Class is meant to be injected into test class.
For single class, it should be instantiated only once per assembly (as the embedded resources do not change during runtime).
Constructors
EmbeddedResourceReader()
Create new instance of the embedded resource reader for calling assembly.
public EmbeddedResourceReader()
EmbeddedResourceReader(Assembly)
Create new instance of the embedded resource reader for given assembly.
public EmbeddedResourceReader(Assembly embeddedResourceAssembly)
Parameters
embeddedResourceAssembly
AssemblyAssembly the embedded resources should be read from.
Exceptions
- ArgumentNullException
Thrown when no
embeddedResourceAssembly
provided.
Properties
CodeDomProvider
Code domain provider used for obtaining correct name of the embedded resource from give file path (see ReadResourceFile(string)).
protected CodeDomProvider CodeDomProvider { get; }
Property Value
- CodeDomProvider
EmbeddedResourceAssembly
Assembly the resources are located in (not the assembly the EmbeddedResourceReader<TAnySourceAssemblyType> is in).
protected Assembly EmbeddedResourceAssembly { get; }
Property Value
- Assembly
Remarks
Value is read only once per instance.
ManifestEmbeddedResourceFileNames
Collections of all embedded resource file names in the EmbeddedResourceAssembly.
protected IEnumerable<string> ManifestEmbeddedResourceFileNames { get; }
Property Value
- IEnumerable<string>
Remarks
Value is read only once per instance.
Methods
CopyBytesToFileSystem(string, string)
Copies content of embedded resource specified by resourceRelativeFilePath
that is stored in the EmbeddedResourceAssembly to file system.
public void CopyBytesToFileSystem(string resourceRelativeFilePath, string targetFullPath)
Parameters
resourceRelativeFilePath
stringRelative path to a single embedded resource (including folders).
targetFullPath
stringFull path to file being saved in file system.
GetResourceStream(string)
Returns content of embedded resource specified by relativeFilePath
that is stored in the EmbeddedResourceAssembly.
public Stream GetResourceStream(string relativeFilePath)
Parameters
relativeFilePath
stringRelative path to a single embedded resource (including folders).
Returns
- Stream
Remarks
relativeFilePath
is supposed to be relative against (executing) project root.
relativeFilePath
has to contain extension.
If there are more resource which name ends with relativeFilePath
,
very first found is returned (see System.Reflection.Assembly.GetManifestResourceNames() for further insights).
In order to obtain correct (partial) embedded resource name, symbols "\" and other invalid characters are replaced with "." and "_" respectively
in the relativeFilePath
.
Exceptions
- ArgumentException
Thrown when null or empty
relativeFilePath
provided.- EmbeddedResourceNotFoundException
Thrown when either embedded resource full name or resource stream was not found.
ReadResourceFile(string)
Returns content of embedded resource specified by relativeFilePath
that is stored in the EmbeddedResourceAssembly.
public string ReadResourceFile(string relativeFilePath)
Parameters
relativeFilePath
stringRelative path to a single embedded resource (including folders).
Returns
- string
Remarks
relativeFilePath
is supposed to be relative against (executing) project root.
relativeFilePath
has to contain extension.
If there are more resource which name ends with relativeFilePath
,
very first found is returned (see System.Reflection.Assembly.GetManifestResourceNames() for further insights).
In order to obtain correct (partial) embedded resource name, symbols "\" and other invalid characters are replaced with "." and "_" respectively
in the relativeFilePath
.
Exceptions
- ArgumentException
Thrown when null or empty
relativeFilePath
provided.- EmbeddedResourceNotFoundException
Thrown when either embedded resource full name or resource stream was not found.
ReadResourceFile(string, string)
Returns content of embedded resource specified by fileName
that is stored in the EmbeddedResourceAssembly under rootFolder
.
public string ReadResourceFile(string rootFolder, string fileName)
Parameters
rootFolder
stringRelative path to the folder where various embedded resources are stored in.
fileName
stringName of a single embedded resource stored within
rootFolder
.
Returns
- string
Remarks
rootFolder
is supposed to be relative against (executing) project root.
fileName
has to contain extension (but must not contain rootFolder
).
If there are more resources which name ends with combination of provided rootFolder
and fileName
,
very first found is returned (see System.Reflection.Assembly.GetManifestResourceNames() for further insights).
In order to obtain correct (partial) embedded resource name, symbols "\" and other invalid characters are replaced with "." and "_" respectively
in the combination of provided rootFolder
and fileName
.
Exceptions
- ArgumentException
Thrown when
rootFolder
orfileName
is null or empty.- EmbeddedResourceNotFoundException
Thrown when either embedded resource full name or resource stream was not found.