Пример #1
0
        private AssemblyLoadResult LoadFromPathInternal(string fullPath)
        {
            Assembly assembly;

            lock (ReferencesLock)
            {
                if (loadedAssembliesByPath.TryGetValue(fullPath, out assembly))
                {
                    LoadedAssembly loadedAssembly = loadedAssemblies[assembly];
                    if (loadedAssembly.LoadedExplicitly)
                    {
                        return(AssemblyLoadResult.CreateAlreadyLoaded(assembly.Location, loadedAssembly.OriginalPath));
                    }
                    else
                    {
                        loadedAssembly.LoadedExplicitly = true;
                        return(AssemblyLoadResult.CreateSuccessful(assembly.Location, loadedAssembly.OriginalPath));
                    }
                }
            }

            AssemblyLoadResult result;

            assembly = ShadowCopyAndLoadAssembly(fullPath, out result);
            if (assembly == null)
            {
                throw new FileNotFoundException(message: null, fileName: fullPath);
            }

            return(result);
        }
Пример #2
0
        private Assembly ShadowCopyAndLoadAndAddEntry(string originalPath, out AssemblyLoadResult result, bool explicitLoad)
        {
            Assembly assembly = ShadowCopyAndLoad(originalPath);

            if (assembly == null)
            {
                result = default(AssemblyLoadResult);
                return(null);
            }

            lock (ReferencesLock)
            {
                // Always remember the path. The assembly might have been loaded from another path or not loaded yet.
                loadedAssembliesByPath[originalPath] = assembly;

                LoadedAssembly loadedAssembly;
                if (loadedAssemblies.TryGetValue(assembly, out loadedAssembly))
                {
                    // The assembly has been loaded while we were copying the file and loading the copy;
                    // Ignore the copy (unfortunately we can't unload a loaded assembly) and use the
                    // existing one.
                    if (loadedAssembly.LoadedExplicitly)
                    {
                        result = AssemblyLoadResult.CreateAlreadyLoaded(assembly.Location, loadedAssembly.OriginalPath);
                    }
                    else
                    {
                        loadedAssembly.LoadedExplicitly = explicitLoad;
                        result = AssemblyLoadResult.CreateSuccessful(assembly.Location, loadedAssembly.OriginalPath);
                    }

                    return(assembly);
                }
                else
                {
                    result = AssemblyLoadResult.CreateSuccessful(assembly.Location, assembly.GlobalAssemblyCache ? assembly.Location : originalPath);
                    loadedAssemblies.Add(assembly, new LoadedAssembly {
                        LoadedExplicitly = explicitLoad, OriginalPath = result.OriginalPath
                    });
                }

                RegisterLoadedAssemblySimpleNameNoLock(assembly);
            }

            if (shadowCopyProvider != null && assembly.GlobalAssemblyCache)
            {
                shadowCopyProvider.SuppressShadowCopy(originalPath);
            }

            return(assembly);
        }