internal Assembly ResolveResourceAssembly(string path, bool failOnMissingAssembly)
        {
            if (assemblies == null)
            {
                lock (SyncRoot) {
                    if (assemblies == null)
                    {
                        assemblies = new Dictionary <string, Assembly>();
                        AppDomain.CurrentDomain.AssemblyLoad += delegate { newAssembliesLoaded = true; };
                    }
                }
            }

            var assemblyName = ResourceUrl.GetEmbeddedResourceAssemblyName(path);
            var assembly     = GetAssemblyByName(assemblyName);

            if (assembly == null)
            {
                if (newAssembliesLoaded)
                {
                    lock (SyncRoot) {
                        if (newAssembliesLoaded)
                        {
                            // add loaded assemblies to cache
                            newAssembliesLoaded = false;
                            foreach (var domainAssembly in AppDomain.CurrentDomain.GetAssemblies())
                            {
                                // replace if duplicated (can happen)
                                assemblies[domainAssembly.GetName().Name] = domainAssembly;
                            }
                        }
                    }
                }

                assembly = GetAssemblyByName(assemblyName);
                if (assembly == null)
                {
                    try {
                        // try load assembly from its name
                        var assemblyPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, assemblyName + ".dll");
                        assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyPath);
                    } catch (IOException) {
                        // ignore
                    }

                    if (assembly != null)
                    {
                        lock (SyncRoot) {
                            assemblies[assembly.GetName().Name] = assembly;
                        }
                    }
                }
            }

            if (failOnMissingAssembly && assembly == null)
            {
                throw new InvalidOperationException("Could not find assembly for: " + path);
            }
            return(assembly);
        }
Beispiel #2
0
        internal static Stream TryGetResource(string url, bool failOnMissingAssembly, out string extension)
        {
            var resourceAssembly = cache.ResolveResourceAssembly(url, failOnMissingAssembly);

            if (resourceAssembly == null)
            {
                extension = string.Empty;
                return(null);
            }
            var resourcePath = ResourceUrl.GetEmbeddedResourcePath(url);

            extension = Path.GetExtension(resourcePath.Last()).ToLower();
            var resourceStream = TryGetResourceWithFullPath(resourceAssembly, resourcePath);

            return(resourceStream);
        }