86 lines
2.6 KiB
C#
86 lines
2.6 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Resources;
|
|
using JD.Kernel.StaticHelpers;
|
|
|
|
namespace JD.TeaBoxAddinSystem.Addins.Description.FileReferences
|
|
{
|
|
/// <summary>
|
|
/// A reference to a resource file embedded in an assembly.
|
|
/// </summary>
|
|
public class AssemblyResourceFileReference : ResourceFileReference
|
|
{
|
|
#region Variables
|
|
|
|
private AssemblyFileReference m_owner;
|
|
|
|
#endregion
|
|
|
|
#region Constructor
|
|
|
|
/// <summary>
|
|
/// Constructor.
|
|
/// </summary>
|
|
/// <param name="filename"></param>
|
|
public AssemblyResourceFileReference(string filename)
|
|
: base(filename)
|
|
{
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region Properties
|
|
|
|
/// <summary>
|
|
/// Owner of this resource file reference.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Set automatically when adding an <see cref="AssemblyResourceFileReference"/> to an <see cref="AssemblyFileReference"/>.
|
|
/// </remarks>
|
|
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
|
|
|
|
/// <summary>
|
|
/// Overriden.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
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
|
|
}
|
|
}
|