using System; using System.Linq; using System.Resources; using JD.Kernel.StaticHelpers; namespace JD.TeaBoxAddinSystem.Addins.Description.FileReferences { /// /// A reference to a resource file embedded in an assembly. /// public class AssemblyResourceFileReference : ResourceFileReference { #region Variables private AssemblyFileReference m_owner; #endregion #region Constructor /// /// Constructor. /// /// public AssemblyResourceFileReference(string filename) : base(filename) { } #endregion #region Properties /// /// Owner of this resource file reference. /// /// /// Set automatically when adding an to an . /// public AssemblyFileReference Owner { get { return m_owner; } internal set { Throw.InvalidOperationIf(m_owner != null && value != null, "This AssemblyResourceFileReference is already owned by another AssemblyFileReference."); m_owner = value; } } #endregion #region Methods /// /// Overriden. /// /// protected override ResourceManager LoadImpl() { if (Owner != null && Owner.Runtime.Assembly != null) { string baseName = Filename; if (baseName.EndsWith(ResourcesExtension, StringComparison.Ordinal)) { baseName = baseName.Substring(0, baseName.Length - ResourcesExtension.Length); } var assembly = Owner.Runtime.Assembly; var existingResources = assembly.GetManifestResourceNames(); if (!existingResources.Contains(baseName + ResourcesExtension, StringComparer.OrdinalIgnoreCase)) { throw new InvalidProgramException( $"Could not find any resource named {baseName + ResourcesExtension} in assembly {assembly.FullName}. Available resources are: '{string.Join(", ", existingResources)}'"); } return new ResourceManager(baseName, assembly); } return null; } #endregion } }