/// <summary>
        /// Adds the Dependency given the Assembly File <paramref name="path"/>.
        /// Loads a Load Context given the component class type host Assembly.
        /// </summary>
        /// <param name="path"></param>
        /// <returns>Returns the Dependency Context instance.</returns>
        internal CompilationAssemblyResolverDependencyContext AddDependency(string path)
        {
            // TODO: TBD: in which case, we are looking for an actual, honest to goodness, Assembly file name path...
            var assembly = LoadContext.LoadFromAssemblyPath(path);

            return(AddDependency(path, assembly));
        }
Exemplo n.º 2
0
        private IList <Assembly> GetModuleAssemblies(string root, string modulePrefix = null)
        {
            if (string.IsNullOrWhiteSpace(modulePrefix))
            {
                modulePrefix = TryGetPrefix();
            }

            if (string.IsNullOrWhiteSpace(modulePrefix))
            {
                throw new ArgumentNullException(nameof(modulePrefix));
            }

            var allLib     = DependencyContext.Default.CompileLibraries;
            var libs       = allLib.Where(x => x.Name.StartsWith(modulePrefix, StringComparison.OrdinalIgnoreCase));
            var assemblies = libs.Select(lib =>
            {
                try
                {
                    return(LoadContext.LoadFromAssemblyName(new AssemblyName(lib.Name)));
                }
                catch (Exception)
                {
                    return(null);
                }
            }
                                         ).Where(x => x != null).ToList();

            var moduleFiles = Directory.GetFiles(root, modulePrefix + "*.dll");

            foreach (var moduleFile in moduleFiles)
            {
                var fileInfo = new FileInfo(moduleFile);
                var theOne   = assemblies.SingleOrDefault(x => fileInfo.Name.Equals(x.GetName().Name + ".dll", StringComparison.OrdinalIgnoreCase));
                if (theOne == null)
                {
                    var assembly = LoadContext.LoadFromAssemblyPath(fileInfo.FullName);
                    assemblies.Add(assembly);
                }
            }

            return(assemblies);
        }