Feedback

C# - ResourcenManager

Veröffentlicht von am 5/20/2014
(0 Bewertungen)
Lädt bei Bedarf bestimmte DLL zur Laufzeit hinzu.
public sealed class DLLResourcesHelper
{
	[DebuggerDisplay("Key={Key}")]
	private class ResourceDLL
	{
		public string Key;
		public byte[] DLL;
		public byte[] PDB;
	}

	private readonly List<ResourceDLL> enabledDLL;
	private bool onceRegistered = false;
	public event Action<string> ResourceLoaded;

	private static readonly Lazy<DLLResourcesHelper> lazyInstance = new Lazy<DLLResourcesHelper>(() => new DLLResourcesHelper());

	private DLLResourcesHelper()
	{
		enabledDLL = new List<ResourceDLL>();
	}

	public static DLLResourcesHelper Instance
	{ 
		get { return lazyInstance.Value; } 
	}

	public void RegisterDLL(string key, byte[] dll, byte[] pdb)
	{
		if (!onceRegistered)
		{
			AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainOnAssemblyResolve;
			onceRegistered = true;
		}

		if (!enabledDLL.Any(e => e.Key.Equals(key)))
		{
			enabledDLL.Add(new ResourceDLL { Key = key, DLL = dll, PDB = pdb });
		}
		else
		{
			throw new Exception("Der angegebene Key wurde bereits verwendet (Führen Sie RegisterDLL aus einem statischen Kontruktor aus, um Mehrfachaufrufe zu verhinden).");
		}
	}

	private Assembly CurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs e)
	{
		Assembly result = null;
		string dllName = e.Name.Contains(',')
			? e.Name.Substring(0, e.Name.IndexOf(','))
			: e.Name.Replace(".dll", "");

		dllName = dllName.Replace(".", "_");
		var content = enabledDLL.FirstOrDefault(r => r.Key.Equals(dllName));

		if(content != null)
		{
			try
			{
				var hasContent = content.DLL != null && content.DLL.Length > 0;
				var hasPdb = content.PDB != null && content.PDB.Length > 0;
				if (hasContent && hasPdb)
				{
					result = Assembly.Load(content.DLL, content.PDB);
				}
				else if (hasContent)
				{
					result = Assembly.Load(content.DLL);
				}

				var handler = ResourceLoaded;
				if (handler != null)
				{
					handler(dllName);
				}
			}
			catch (Exception ex)
			{
				Trace.WriteLine(ex.ToString());
			}
		}
		return result;
	}
}
Abgelegt unter DLL, Laufzeit, Runtime, Laden, Klassenbibliothek.

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!