Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Reflection;
- using System.Security.Cryptography;
- /// <summary>
- /// Static class for loaded managed and unmanaged assemblies from embedded resources.
- /// </summary>
- public static class EmbeddedAssembly
- {
- private static Dictionary<string, Assembly> _dictionary = null;
- /// <summary>
- /// Gets a dictionary of assemblies loaded from memory, where each key is the
- /// full name of the assembly to be retrieved.
- /// </summary>
- public static Dictionary<string, Assembly> Assemblies
- {
- get
- {
- if (_dictionary == null)
- _dictionary = new Dictionary<string, Assembly>();
- return _dictionary;
- }
- }
- /// <summary>
- /// Loads a managed/unmanaged assembly that is embedded as a resource file.
- /// </summary>
- /// <param name="resourceName">The full name of the resource file.</param>
- /// <param name="filename">Filename</param>
- public static void Load(string resourceName, string filename)
- {
- if (_dictionary == null)
- _dictionary = new Dictionary<string, Assembly>();
- byte[] bytes;
- var currentAssembly = Assembly.GetExecutingAssembly();
- using (var stream = currentAssembly.GetManifestResourceStream(resourceName))
- {
- if (stream == null)
- throw new Exception(resourceName + " is not a valid resource.");
- bytes = new byte[(int)stream.Length];
- stream.Read(bytes, 0, (int)stream.Length);
- }
- var loaded = LoadManaged(bytes);
- if (!loaded)
- loaded = LoadUnmanaged(bytes, filename);
- if (!loaded)
- throw new Exception(resourceName + " could not be loaded from memory.");
- }
- /// <summary>
- /// Attempts to load a managed assembly from memory.
- /// </summary>
- /// <param name="bytes">Byte array of the assembly's resource stream.</param>
- /// <returns>Flag indicating if load was successful.</returns>
- private static bool LoadManaged(byte[] bytes)
- {
- try
- {
- var assembly = Assembly.Load(bytes);
- _dictionary.Add(assembly.FullName, assembly);
- return true;
- }
- catch { return false; }
- }
- /// <summary>
- /// Attempts to load an unmanaged assembly from memory.
- /// </summary>
- /// <param name="bytes">Byte array if the assemblie's resource stream.</param>
- /// <param name="filename">Filename assembly will be temporarily saved as.</param>
- /// <returns>Flag indicating if load was successful.</returns>
- private static bool LoadUnmanaged(byte[] bytes, string filename)
- {
- var tempFile = Path.Combine(Path.GetTempPath(), filename);
- if (File.Exists(tempFile))
- {
- // Confirm files are exactly the same, overwrite existing file if they are not.
- using (var sha1 = new SHA1CryptoServiceProvider())
- {
- var hash = BitConverter.ToString(sha1.ComputeHash(bytes)).Replace("-", String.Empty);
- var tempFileBytes = File.ReadAllBytes(tempFile);
- var tempHash = BitConverter.ToString(sha1.ComputeHash(tempFileBytes)).Replace("-", string.Empty);
- if (hash != tempHash)
- File.WriteAllBytes(tempFile, tempFileBytes);
- }
- }
- else
- File.WriteAllBytes(tempFile, bytes);
- try
- {
- var assembly = Assembly.LoadFile(tempFile);
- _dictionary.Add(assembly.FullName, assembly);
- return true;
- }
- catch { return false; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement