Created
January 9, 2016 21:19
-
-
Save kiwipiet/7a5a596dca12f5623273 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// <summary> | |
| /// Easily get access to embedded resources | |
| /// </summary> | |
| internal static class EmbeddedResource | |
| { | |
| /// <summary> | |
| /// Return a resource stream | |
| /// </summary> | |
| /// <param name="assembly">The assembly the resource is embedded in</param> | |
| /// <param name="name">FQDN or the resource</param> | |
| /// <returns>Resource Stream</returns> | |
| public static StreamReader GetStream(Assembly assembly, string name) | |
| { | |
| return (from resName in assembly.GetManifestResourceNames() | |
| where resName.EndsWith(name) | |
| select new StreamReader(assembly.GetManifestResourceStream(resName))) | |
| .FirstOrDefault(); | |
| } | |
| /// <summary> | |
| /// Return a resource stream from the same assembly as <see cref="EmbeddedResource" /> | |
| /// </summary> | |
| /// <param name="name">FQDN or the resource</param> | |
| /// <returns>Resource Stream</returns> | |
| public static StreamReader GetStream(string name) | |
| { | |
| return GetStream(typeof (EmbeddedResource).Assembly, name); | |
| } | |
| /// <summary> | |
| /// Returns a resource string | |
| /// </summary> | |
| /// <param name="assembly">The assembly the resource is embedded in</param> | |
| /// <param name="name">FQDN or the resource</param> | |
| /// <returns>resource string</returns> | |
| public static string GetString(Assembly assembly, string name) | |
| { | |
| using (var sr = GetStream(assembly, name)) | |
| { | |
| var data = sr.ReadToEnd(); | |
| return data; | |
| } | |
| } | |
| /// <summary> | |
| /// Returns a resource string from the same assembly as <see cref="EmbeddedResource" /> | |
| /// </summary> | |
| /// <param name="name">FQDN or the resource</param> | |
| /// <returns>resource string</returns> | |
| public static string GetString(string name) | |
| { | |
| return GetString(typeof (EmbeddedResource).Assembly, name); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment