protected override Assembly?Load(AssemblyName assemblyName) { var simpleName = assemblyName.Name !; if (CompilerAssemblySimpleNames.Contains(simpleName)) { // Delegate to the compiler's load context to load the compiler or anything // referenced by the compiler return(null); } var assemblyPath = Path.Combine(Directory, simpleName + ".dll"); if (!_loader.IsKnownDependencyLocation(assemblyPath)) { // The analyzer didn't explicitly register this dependency. Most likely the // assembly we're trying to load here is netstandard or a similar framework // assembly. We assume that if that is not the case, then the parent ALC will // fail to load this. return(null); } var pathToLoad = _loader.GetPathToLoad(assemblyPath); return(LoadFromAssemblyPath(pathToLoad)); }
protected override Assembly?Load(AssemblyName assemblyName) { var simpleName = assemblyName.Name !; if (_loader.AssemblySimpleNamesToBeLoadedInCompilerContext.Contains(simpleName)) { // Delegate to the compiler's load context to load the compiler or anything // referenced by the compiler return(_compilerLoadContext.LoadFromAssemblyName(assemblyName)); } var assemblyPath = Path.Combine(Directory, simpleName + ".dll"); var paths = _loader.GetPaths(simpleName); if (paths is null) { // The analyzer didn't explicitly register this dependency. Most likely the // assembly we're trying to load here is netstandard or a similar framework // assembly. In this case, we want to load it in compiler's ALC to avoid any // potential type mismatch issue. Otherwise, if this is truly an unknown assembly, // we assume both compiler and default ALC will fail to load it. return(_compilerLoadContext.LoadFromAssemblyName(assemblyName)); } Debug.Assert(paths.Any()); // A matching assembly in this directory was specified via /analyzer. if (paths.Contains(assemblyPath)) { return(LoadFromAssemblyPath(_loader.GetPathToLoad(assemblyPath))); } AssemblyName?bestCandidateName = null; string? bestCandidatePath = null; // The assembly isn't expected to be found at 'assemblyPath', // but some assembly with the same simple name is known to the loader. foreach (var candidatePath in paths) { // Note: we assume that the assembly really can be found at 'candidatePath' // (without 'GetPathToLoad'), and that calling GetAssemblyName doesn't cause us // to hold a lock on the file. This prevents unnecessary shadow copies. var candidateName = AssemblyName.GetAssemblyName(candidatePath); // Checking FullName ensures that version and PublicKeyToken match exactly. if (candidateName.FullName.Equals(assemblyName.FullName, StringComparison.OrdinalIgnoreCase)) { return(LoadFromAssemblyPath(_loader.GetPathToLoad(candidatePath))); } else if (bestCandidateName is null || bestCandidateName.Version < candidateName.Version) { bestCandidateName = candidateName; bestCandidatePath = candidatePath; } } Debug.Assert(bestCandidateName != null); Debug.Assert(bestCandidatePath != null); return(LoadFromAssemblyPath(_loader.GetPathToLoad(bestCandidatePath))); }