// -------------------------------------------------------------------------------------------------------------------- // // Copyright (c) James South. // Licensed under the Apache License, Version 2.0. // // // Encapsulates a series of time saving extension methods to the class. // // -------------------------------------------------------------------------------------------------------------------- namespace ImageProcessor.Common.Extensions { using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; using System.Text; /// /// Encapsulates a series of time saving extension methods to the class. /// public static class AssemblyExtensions { /// /// Gets a collection of loadable types from the given assembly. /// Adapted from /// /// /// The to load the types from. /// /// /// The loadable . /// public static IEnumerable GetLoadableTypes(this Assembly assembly) { if (assembly == null) { throw new ArgumentNullException("assembly"); } try { return assembly.GetTypes(); } catch (ReflectionTypeLoadException ex) { return ex.Types.Where(t => t != null); } } /// /// Converts an assembly resource into a string. /// /// /// The to load the strings from. /// /// /// The resource. /// /// /// The character encoding to return the resource in. /// /// /// The . /// public static string GetResourceAsString(this Assembly assembly, string resource, Encoding encoding = null) { encoding = encoding ?? Encoding.UTF8; using (MemoryStream ms = new MemoryStream()) { using (Stream manifestResourceStream = assembly.GetManifestResourceStream(resource)) { if (manifestResourceStream != null) { manifestResourceStream.CopyTo(ms); } } return encoding.GetString(ms.GetBuffer()).Replace('\0', ' ').Trim(); } } /// /// Returns the identifying the file used to load the assembly /// /// /// The to get the name from. /// /// The public static FileInfo GetAssemblyFile(this Assembly assembly) { string codeBase = assembly.CodeBase; Uri uri = new Uri(codeBase); string path = uri.LocalPath; return new FileInfo(path); } /// /// Returns the identifying the file used to load the assembly /// /// /// The to get the name from. /// /// The public static FileInfo GetAssemblyFile(this AssemblyName assemblyName) { var codeBase = assemblyName.CodeBase; var uri = new Uri(codeBase); var path = uri.LocalPath; return new FileInfo(path); } } }