/// <inheritdoc cref=""/>
        public Assembly LoadAssembly(string assemblyPath)
        {
            if (!CliUtils.IsCliAssembly(assemblyPath))
            {
                return(null);
            }

            var directory = Path.GetDirectoryName(assemblyPath);
            var fileName  = Path.GetFileName(assemblyPath);

            Assembly loadedAssembly = SpeciallyLoadedByAssemblyFileName(fileName);

            if (loadedAssembly == null)
            {
                if (!loadContexts.TryGetValue(directory, out var loadContext))
                {
                    loadContext = new PluginAssemblyLoadContext(directory, this.SpeciallyLoadedByAssemblyName);
                    if (!loadContexts.TryAdd(directory, loadContext))
                    {
                        if (!loadContexts.TryGetValue(directory, out loadContext))
                        {
                            Console.Error.WriteLine($"Unable to load assembly {assemblyPath}.");
                        }
                    }
                }

                if (loadContext != null)
                {
                    try
                    {
                        loadedAssembly = loadContext.LoadFromAssemblyPath(assemblyPath);
                    }
                    catch (BadImageFormatException)
                    {
                        //
                        // this means it is native code or otherwise
                        // not readable by the CLR.
                        //

                        loadedAssembly = null;
                    }
                    catch (FileLoadException e)
                    {
                        Console.Error.WriteLine(
                            "[warn]: managed assembly `{0}` cannot be loaded - {1}.",
                            assemblyPath,
                            e.FusionLog);
                        loadedAssembly = null;
                    }
                    catch (FileNotFoundException)
                    {
                        loadedAssembly = null;
                    }
                }
            }

            return(loadedAssembly);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Loads the specified path as an assembly into the default load context.
        /// </summary>
        /// <param name="assemblyPath">
        ///     Path to an assembly.
        /// </param>
        public Assembly LoadAssembly(string assemblyPath)
        {
            if (!CliUtils.IsCliAssembly(assemblyPath))
            {
                return(null);
            }

            Assembly loadedAssembly;

            try
            {
                loadedAssembly = Assembly.LoadFrom(assemblyPath);
            }
            catch (BadImageFormatException)
            {
                //
                // this means it is native code or otherwise
                // not readable by the CLR.
                //

                loadedAssembly = null;
            }
            catch (FileLoadException e)
            {
                Console.Error.WriteLine(
                    "[warn]: managed assembly `{0}` cannot be loaded - {1}.",
                    assemblyPath,
                    e.FusionLog);
                loadedAssembly = null;
            }
            catch (FileNotFoundException)
            {
                loadedAssembly = null;
            }

            return(loadedAssembly);
        }