public string ExecuteUntrusedCode(Assembly assembly, string[] parameters) { try { MethodInfo untrustedMethod = FindMainMethod(assembly); if (untrustedMethod == null) { throw new Exception(SandboxExceptions.MainNotFound); } ConsoleReader consoleReader = new ConsoleReader(); SetConsoleStreamToConsoleReader(consoleReader); Thread thread = new Thread(() => { try { untrustedMethod.Invoke(null, parameters); } catch (Exception ex) { consoleReader.ConsoleOut = ex.InnerException.Message; } }); thread.Start(); if (!thread.Join(TimeSpan.FromSeconds(TimeOutInSec))) { AbortThread(thread); } consoleReader.Close(); consoleReader.Dispose(); return(consoleReader.ConsoleOut); } catch (Exception ex) { return(ex.Message); } }
private void SetConsoleStreamToConsoleReader(ConsoleReader consoleReader) { Console.SetOut(consoleReader); }