public PluginContainer GetPlugin(string path, ILogger logger) { string name = Path.GetFileNameWithoutExtension(path); FileStream fileStream; try { fileStream = File.OpenRead(path); } catch { logger.LogError($"Reloading '{Path.GetFileName(path)}' failed, file is not accessible."); return(new PluginContainer(new PluginInfo(name), name)); } SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(SourceText.From(fileStream)); fileStream.Dispose(); var compilation = CSharpCompilation.Create(name, new[] { syntaxTree }, MetadataReferences, CompilationOptions); using var memoryStream = new MemoryStream(); EmitResult emitResult = compilation.Emit(memoryStream); if (!emitResult.Success) { if (logger != null) { foreach (var diagnostic in emitResult.Diagnostics) { if (diagnostic.Severity != DiagnosticSeverity.Error || diagnostic.IsWarningAsError) { continue; } logger.LogError($"Compilation failed: {diagnostic.Location} {diagnostic.GetMessage()}"); } } return(new PluginContainer(new PluginInfo(name), name)); } else { memoryStream.Seek(0, SeekOrigin.Begin); var loadContext = new PluginLoadContext(name + "LoadContext", path); var assembly = loadContext.LoadFromStream(memoryStream); return(PluginProviderSelector.CompiledPluginProvider.HandlePlugin(loadContext, assembly, path, logger)); } }