/// <summary> /// Gets the text content of an embedded resource. /// </summary> /// <param name="file">The path in the form: dll://AssemblyName/ResourceName</param> /// <returns>The content of the resource</returns> public static string GetResource(string file, IFileReader fileReader) { var match = Importer.EmbeddedResourceRegex.Match(file); if (!match.Success) return null; var loader = new ResourceLoader(); loader._resourceName = match.Groups["Resource"].Value; loader._assemblyName = match.Groups["Assembly"].Value; loader._fileReader = fileReader; try { var domainSetup = new AppDomainSetup(); domainSetup.ApplicationBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var domain = AppDomain.CreateDomain("LoaderDomain", null, domainSetup); domain.DoCallBack(loader.LoadResource); AppDomain.Unload(domain); } catch (Exception) { throw new FileNotFoundException("Unable to load resource [" + loader._resourceName + "] in assembly [" + loader._assemblyName + "]"); } return loader._resourceContent; }
private static void LoadFromNewAppDomain(ResourceLoader loader, IFileReader fileReader, String assemblyName) { if (!fileReader.DoesFileExist(assemblyName)) { throw new FileNotFoundException("Unable to locate assembly file [" + assemblyName + "]"); } loader._fileContents = fileReader.GetBinaryFileContents(assemblyName); var domainSetup = new AppDomainSetup(); domainSetup.ApplicationBase = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var domain = AppDomain.CreateDomain("LoaderDomain", null, domainSetup); domain.DoCallBack(loader.LoadResource); AppDomain.Unload(domain); }
/// <summary> /// Gets the text content of an embedded resource. /// </summary> /// <param name="file">The path in the form: dll://AssemblyName#ResourceName</param> /// <returns>The content of the resource</returns> public static string GetResource(string file, IFileReader fileReader, out string fileDependency) { fileDependency = null; var match = Importer.EmbeddedResourceRegex.Match(file); if (!match.Success) return null; var loader = new ResourceLoader(); loader._resourceName = match.Groups["Resource"].Value; try { fileDependency = match.Groups["Assembly"].Value; LoadFromCurrentAppDomain(loader, fileDependency); if (String.IsNullOrEmpty(loader._resourceContent)) LoadFromNewAppDomain(loader, fileReader, fileDependency); } catch (Exception) { throw new FileNotFoundException("Unable to load resource [" + loader._resourceName + "] in assembly [" + fileDependency + "]"); } finally { loader._fileContents = null; } return loader._resourceContent; }
private static void LoadFromCurrentAppDomain(ResourceLoader loader, String assemblyName) { foreach (var assembly in AppDomain.CurrentDomain .GetAssemblies() .Where(x => !IsDynamicAssembly(x) && x.Location.EndsWith(assemblyName, StringComparison.InvariantCultureIgnoreCase))) { if (assembly.GetManifestResourceNames().Contains(loader._resourceName)) { using (var stream = assembly.GetManifestResourceStream(loader._resourceName)) using (var reader = new StreamReader(stream)) { loader._resourceContent = reader.ReadToEnd(); if (!String.IsNullOrEmpty(loader._resourceContent)) return; } } } }
private static void LoadFromCurrentAppDomain(ResourceLoader loader, String assemblyName) { #region The assembly already exists in the current app domain, no need to load it again. foreach (var assembly in AppDomain.CurrentDomain .GetAssemblies() .Where(x => x.Location.EndsWith(assemblyName, StringComparison.InvariantCultureIgnoreCase))) { if (assembly.GetManifestResourceNames().Contains(loader._resourceName)) { using (var stream = assembly.GetManifestResourceStream(loader._resourceName)) using (var reader = new StreamReader(stream)) { loader._resourceContent = reader.ReadToEnd(); if (!String.IsNullOrEmpty(loader._resourceContent)) return; } } } #endregion }