private static IReadOnlyList <Assembly> LoadAssemblyFromName(
            AssemblyLoadContext loadContext,
            AssemblyName assemblyName,
            IEnumerable <string> dependencyDirectories,
            Core.ILogger logger = null)
        {
            // If assembly exists in any of the dependency paths then load it from there.
            var dependencyPath = dependencyDirectories.
                                 Select(d =>
            {
                try
                {
                    return
                    (Directory.EnumerateFiles(d, $"{assemblyName.Name}.dll", SearchOption.AllDirectories).
                     FirstOrDefault());
                }
                catch (Exception)
                {
                    return(null);
                }
            }).
                                 Where(p => p != null).
                                 FirstOrDefault();

            if (!string.IsNullOrEmpty(dependencyPath))
            {
                return(LoadAssemblyFromPath(loadContext, dependencyPath, dependencyDirectories, logger));
            }

            // Otherwise load by name from the context.
            var output = new List <Assembly>();

            try
            {
                output.Add(loadContext.LoadFromAssemblyName(assemblyName));
                logger?.LogTrace($"Loaded assembly: '{output[0].FullName}'");
            }
            catch (Exception e)
            {
                if (logger != null)
                {
                    logger?.LogWarning($"Failed to load assembly: {e.Message.ToDistinctLines()}");
                }
                return(Array.Empty <Assembly>());
            }

            // Load referenced assemblies.
            foreach (var referencedAssemblyName in output[0].GetReferencedAssemblies())
            {
                if (!IsAssemblyLoaded(referencedAssemblyName))
                {
                    output.AddRange(
                        LoadAssemblyFromName(loadContext, referencedAssemblyName, dependencyDirectories, logger));
                }
            }

            return(output);
        }
        private static IReadOnlyList <Assembly> LoadAssemblyFromPath(
            AssemblyLoadContext loadContext,
            string path,
            IEnumerable <string> dependencyDirectories,
            Core.ILogger logger = null)
        {
            // Validate path.
            var validatedPath = ValidateFilePath(path, logger);

            if (validatedPath == null)
            {
                return(Array.Empty <Assembly>());
            }

            // Load assembly.
            var output = new List <Assembly>();

            try
            {
                output.Add(loadContext.LoadFromAssemblyPath(validatedPath));
                logger?.LogTrace($"Loaded assembly: '{output[0].FullName}'");
            }
            catch (Exception e)
            {
                if (logger != null)
                {
                    logger?.LogWarning($"Failed to load assembly: {e.Message.ToDistinctLines()}");
                }
                return(Array.Empty <Assembly>());
            }

            // Load referenced assemblies.
            foreach (var referencedAssemblyName in output[0].GetReferencedAssemblies())
            {
                if (!IsAssemblyLoaded(referencedAssemblyName))
                {
                    output.AddRange(
                        LoadAssemblyFromName(loadContext, referencedAssemblyName, dependencyDirectories, logger));
                }
            }

            return(output);
        }