public object RunCommand(string command, bool addToOutput)
        {
            if (command == "")
            {
                return(null);
            }

            if (addToOutput)
            {
                this.FindControl <StackPanel>("consoleContainer").Children.Insert(this.FindControl <StackPanel>("consoleContainer").Children.Count - 1, new HistoryLine(command));
            }

            bool toBeCollected = false;

            try
            {
                Task <ScriptState <object> > evalTask;

                if (lastTask == null)
                {
                    evalTask = CSharpScript.RunAsync(command);
                }
                else
                {
                    evalTask = lastTask.Result.ContinueWithAsync(command);
                }

                evalTask.Wait();

                lastTask = evalTask;

                if (!(evalTask.Result.ReturnValue is DoNotAddOutputLine) && command != "Clear();")
                {
                    if (addToOutput)
                    {
                        GlobalsContainer.Ans = evalTask.Result.ReturnValue;
                        if (!(evalTask.Result.ReturnValue is Type))
                        {
                            this.FindControl <StackPanel>("consoleContainer").Children.Insert(this.FindControl <StackPanel>("consoleContainer").Children.Count - 1, new CSharpOutputLine(evalTask.Result.ReturnValue, false));
                        }
                        else
                        {
                            this.FindControl <StackPanel>("consoleContainer").Children.Insert(this.FindControl <StackPanel>("consoleContainer").Children.Count - 1, new CSharpObjectStructure((Type)evalTask.Result.ReturnValue, "", false, null));
                        }
                    }
                }

                if (evalTask.Result.ReturnValue is ResetType || command == "Reset();")
                {
                    ___Reset();
                    toBeCollected = true;
                }

                if (command.Contains("#r"))
                {
                    string[] commandLines = command.Replace("\r", "").Split('\n');
                    foreach (string commandLine in commandLines)
                    {
                        string cmdLine = commandLine.TrimStart(' ', '\t');
                        if (cmdLine.StartsWith("#r"))
                        {
                            try
                            {
                                string assemblyName = cmdLine.Substring(cmdLine.IndexOf("\"") + 1);
                                assemblyName = assemblyName.Substring(0, assemblyName.LastIndexOf("\""));
                                assemblyName = assemblyName.TrimStart(' ', '\t').TrimEnd(' ', '\t');

                                try
                                {
                                    Assembly.LoadFrom(assemblyName);
                                }
                                catch
                                {
                                    try
                                    {
                                        Assembly.LoadWithPartialName(assemblyName);
                                    }
                                    catch (Exception ex)
                                    {
                                        Exception reportedEx = new AssemblyLoadException("An error occurred while loading assembly " + assemblyName + ": its types will be available in code but not during structure exploration.", ex, assemblyName);
                                        this.FindControl <StackPanel>("consoleContainer").Children.Insert(this.FindControl <StackPanel>("consoleContainer").Children.Count - 1, new ErrorLine(reportedEx, true));
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                Exception reportedEx = new AssemblyNameParseException("An error occurred while trying to parse an assembly name from a preprocessor directive: if it was a valid assembly, its types will be available in code but not during structure exploration.", ex, cmdLine);
                                this.FindControl <StackPanel>("consoleContainer").Children.Insert(this.FindControl <StackPanel>("consoleContainer").Children.Count - 1, new ErrorLine(reportedEx, true));
                            }
                        }
                    }
                }

                new Thread(() =>
                {
                    Thread.Sleep(10);

                    if (addToOutput)
                    {
                        Dispatcher.UIThread.InvokeAsync(() =>
                        {
                            this.FindControl <ScrollViewer>("scroller").Offset = new Vector(this.FindControl <ScrollViewer>("scroller").Offset.X, double.MaxValue);
                        });
                    }

                    if (toBeCollected)
                    {
                        GC.Collect();
                    }
                }).Start();

                return(evalTask.Result.ReturnValue);
            }
            catch (Exception ex)
            {
                if (addToOutput)
                {
                    GlobalsContainer.Ans = ex;
                    this.FindControl <StackPanel>("consoleContainer").Children.Insert(this.FindControl <StackPanel>("consoleContainer").Children.Count - 1, new ErrorLine(ex, false));

                    new Thread(() =>
                    {
                        Thread.Sleep(10);

                        if (addToOutput)
                        {
                            Dispatcher.UIThread.InvokeAsync(() =>
                            {
                                this.FindControl <ScrollViewer>("scroller").Offset = new Vector(this.FindControl <ScrollViewer>("scroller").Offset.X, double.MaxValue);
                            });
                        }

                        if (toBeCollected)
                        {
                            GC.Collect();
                        }
                    }).Start();
                }
                throw ex;
            }
        }
        private void Initialize(string[] preloadedAssemblies = null)
        {
            CustomLoadedAssemblies = preloadedAssemblies;

            RobotoMono = FontFamily.Parse("resm:ScriptConsoleLibrary.Fonts.?assembly=ScriptConsoleLibrary#Roboto Mono");

            GlobalsContainer.consoleContainer   = this.FindControl <StackPanel>("consoleContainer");
            GlobalsContainer._____ConsoleWriter = new OutputConsoleWriter(this.FindControl <StackPanel>("consoleContainer"));
            GlobalsContainer.Ans           = null;
            GlobalsContainer.cSharpConsole = this;
            globalsContainer = new GlobalsContainer();
            lastTask         = CSharpScript.RunAsync("System.Console.SetOut(_____ConsoleWriter);", null, globalsContainer);
            lastTask.Wait();

            foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
            {
                bool toBeLoaded = false;

                try
                {
                    if (preloadedAssemblies == null)
                    {
                        if (CoreLoadedAssemblies.Contains(asm.GetName().Name))
                        {
                            toBeLoaded = true;
                        }
                    }
                    else
                    {
                        if (preloadedAssemblies.Contains(asm.GetName().Name))
                        {
                            toBeLoaded = true;
                        }
                    }
                }
                catch
                {
                }

                if (toBeLoaded)
                {
                    try
                    {
                        lastTask = lastTask.Result.ContinueWithAsync("#r \"" + asm.FullName + "\"");
                        lastTask.Wait();
                    }
                    catch
                    {
                        try
                        {
                            lastTask = lastTask.Result.ContinueWithAsync("#r \"" + asm.Location + "\"");
                            lastTask.Wait();
                        }
                        catch (Exception ex)
                        {
                            Exception reportedEx;
                            try
                            {
                                reportedEx = new AssemblyLoadException("An error occurred while loading assembly " + asm.GetName().Name + ": its types will be available during structure exploration but not in code.", ex, asm);
                            }
                            catch
                            {
                                reportedEx = new AssemblyLoadException("An error occurred while loading an assembly.", ex, asm);
                            }

                            this.FindControl <StackPanel>("consoleContainer").Children.Insert(this.FindControl <StackPanel>("consoleContainer").Children.Count - 1, new ErrorLine(reportedEx, true));
                        }
                    }
                }
            }
        }
        private void ___Reset()
        {
            this.FindControl <StackPanel>("consoleContainer").Children.Clear();
            this.FindControl <StackPanel>("consoleContainer").Children.Add(this.FindControl <ConsoleInputLine>("inputLine"));
            this.lastTask = null;
            GlobalsContainer.consoleContainer   = this.FindControl <StackPanel>("consoleContainer");
            GlobalsContainer._____ConsoleWriter = new OutputConsoleWriter(this.FindControl <StackPanel>("consoleContainer"));
            GlobalsContainer.Ans           = null;
            GlobalsContainer.cSharpConsole = this;
            this.globalsContainer          = new GlobalsContainer();
            this.lastTask = CSharpScript.RunAsync("System.Console.SetOut(_____ConsoleWriter);", null, this.globalsContainer);
            this.lastTask.Wait();

            foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
            {
                bool toBeLoaded = false;

                try
                {
                    if (CustomLoadedAssemblies == null)
                    {
                        if (CoreLoadedAssemblies.Contains(asm.GetName().Name))
                        {
                            toBeLoaded = true;
                        }
                    }
                    else
                    {
                        if (CustomLoadedAssemblies.Contains(asm.GetName().Name))
                        {
                            toBeLoaded = true;
                        }
                    }
                }
                catch
                {
                }

                if (toBeLoaded)
                {
                    try
                    {
                        this.lastTask = this.lastTask.Result.ContinueWithAsync("#r \"" + asm.FullName + "\"");
                        this.lastTask.Wait();
                    }
                    catch
                    {
                        try
                        {
                            this.lastTask = this.lastTask.Result.ContinueWithAsync("#r \"" + asm.Location + "\"");
                            this.lastTask.Wait();
                        }
                        catch (Exception ex)
                        {
                            Exception reportedEx;
                            try
                            {
                                reportedEx = new AssemblyLoadException("An error occurred while loading assembly " + asm.GetName().Name + ": its types will be available during structure exploration but not in code.", ex, asm);
                            }
                            catch
                            {
                                reportedEx = new AssemblyLoadException("An error occurred while loading an assembly.", ex, asm);
                            }

                            this.FindControl <StackPanel>("consoleContainer").Children.Insert(this.FindControl <StackPanel>("consoleContainer").Children.Count - 1, new ErrorLine(reportedEx, true));
                        }
                    }
                }
            }

            this.history.Clear();
        }