Exemplo n.º 1
0
        /// <summary>
        /// Attempt to load the specified Assembly from its full name or location on the hard drive.
        /// </summary>
        /// <param name="assemblyStream">The assembly stream</param>
        /// <param name="forReflectionPurpose">(optional) Defines whether the assembly must be load for reflection only or also execution. By default, the value is true.</param>
        public void LoadAssembly(MemoryStream assemblyStream, bool forReflectionPurpose = true)
        {
            Requires.NotNull(assemblyStream, nameof(assemblyStream));

            if (!forReflectionPurpose && !AssemblyInfoHelper.IsDotNetAssembly(assemblyStream))
            {
                throw new NotSupportedException("A unmanaged library cannot be loaded from a memory stream.");
            }

            assemblyStream.Seek(0, SeekOrigin.Begin);

            _assemblyManager.LoadAssembly(assemblyStream.ToArray(), forReflectionPurpose);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Attempts ReflectionOnlyLoad of current Assemblies dependants
        /// </summary>
        /// <param name="args">ReflectionOnlyAssemblyResolve event args</param>
        /// <param name="directory">The current Assemblies Directory</param>
        /// <returns>ReflectionOnlyLoadFrom loaded dependant Assembly</returns>
        private Assembly OnReflectionOnlyResolve(ResolveEventArgs args, DirectoryInfo directory)
        {
            var loadedAssembly = GetAssembliesInternal().FirstOrDefault(asm => string.Equals(asm.Assembly.FullName, args.Name, StringComparison.OrdinalIgnoreCase));

            if (loadedAssembly != null)
            {
                return(loadedAssembly.Assembly);
            }

            var assemblyName = new AssemblyName(args.Name);

            if (directory != null)
            {
                var dependentAssemblyFilenames = new string[] { Path.Combine(directory.FullName, assemblyName.Name + ".dll"), Path.Combine(directory.FullName, assemblyName.Name + ".exe") };

                foreach (var dependentAssemblyFilename in dependentAssemblyFilenames)
                {
                    if (File.Exists(dependentAssemblyFilename))
                    {
                        var assembly = Assembly.ReflectionOnlyLoadFrom(dependentAssemblyFilename);
                        _explicitLoadedAssemblies.Add(new LoadedAssemblyDetails
                        {
                            Assembly = assembly,
                            Details  = AssemblyInfoHelper.GetAssemblyDetailsFromNameOrLocation(dependentAssemblyFilename)
                        });
                        return(assembly);
                    }
                }
            }

            var assembly2 = Assembly.ReflectionOnlyLoad(assemblyName.FullName);

            _explicitLoadedAssemblies.Add(new LoadedAssemblyDetails
            {
                Assembly = assembly2,
                Details  = AssemblyInfoHelper.GetAssemblyDetailsFromNameOrLocation(assemblyName.FullName)
            });
            return(assembly2);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Attempt to load the specified Assembly.
        /// </summary>
        /// <param name="assemblyByteArray">A byte array that represents the assembly.</param>
        /// <param name="forReflectionPurpose">Defines whether the assembly must be load for reflection only or also execution.</param>
        internal void LoadAssembly(byte[] assemblyByteArray, bool forReflectionPurpose)
        {
            Assembly assembly = null;

            if (forReflectionPurpose)
            {
                assembly = Assembly.ReflectionOnlyLoad(assemblyByteArray);
            }
            else
            {
                assembly = Assembly.Load(assemblyByteArray);
            }

            var details = AssemblyInfoHelper.GetAssemblyDetailsFromNameOrLocation(assembly.FullName);

            details.ProcessorArchitecture = assembly.GetName().ProcessorArchitecture;

            _explicitLoadedAssemblies.Add(new LoadedAssemblyDetails
            {
                Assembly = assembly,
                Details  = details
            });
        }
Exemplo n.º 4
0
 /// <summary>
 /// Attempt to load the specified Assembly from its full name or location on the hard drive.
 /// </summary>
 /// <param name="assemblyPath">The assembly's full name or location on the hard drive</param>
 /// <param name="forReflectionPurpose">(optional) Defines whether the assembly must be load for reflection only or also execution. By default, the value is true.</param>
 public void LoadAssembly(string assemblyPath, bool forReflectionPurpose = true)
 {
     Requires.NotNullOrWhiteSpace(assemblyPath, nameof(assemblyPath));
     LoadAssembly(AssemblyInfoHelper.GetAssemblyDetailsFromNameOrLocation(assemblyPath), forReflectionPurpose);
 }