public static void Execute()
            {
                if (_assembly == null)
                {
                    return;
                }

                UserCodeExecutionContext executionContext = AppDomain.CurrentDomain.GetData("executionContext") as UserCodeExecutionContext;
                var type   = _assembly.GetType("UserCodeClass");
                var method = type.GetMethod("<Factory>", BindingFlags.Static | BindingFlags.Public);

                method.Invoke(null, new object[] { new object[2] {
                                                       executionContext, null
                                                   } });
                AppDomain.CurrentDomain.SetData("executionContext", executionContext);
            }
        private void cmdGo_Click(object sender, RoutedEventArgs e)
        {
            Script script = Setup();
            var    comp   = script.GetCompilation();

            comp = comp.WithOptions(comp.Options.WithOutputKind(OutputKind.DynamicallyLinkedLibrary)
                                    .WithPlatform(Platform.X64)
                                    .WithOptimizationLevel(OptimizationLevel.Debug)
                                    .WithModuleName("UserCode")
                                    .WithScriptClassName("UserCodeClass")
                                    ).WithAssemblyName("UserCode");

            if (_assembly != null)
            {
                _assembly = null;
                AppDomain.Unload(_appDomain);
                _appDomain = null;

                // are these 3 lines needed??? test thoroughly!
                GC.Collect();
                GC.WaitForPendingFinalizers();
                GC.Collect();
                // force-kill vsdbg-ui.exe? or tell the user to Detach Or Else?
            }

            using (var modFS = new FileStream("UserCode.dll", FileMode.Create))
                using (var pdbFS = new FileStream("UserCode.pdb", FileMode.Create))
                {
                    EmitResult result = comp.Emit(modFS, pdbFS, options: new EmitOptions(false, DebugInformationFormat.PortablePdb));
                    if (result.Success)
                    {
                        Logger.Write("Compiled and saved!");
                    }
                    else
                    {
                        Logger.Write("Compile failed! Diagnostics follow");
                        IEnumerable <Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
                                                                                     diagnostic.IsWarningAsError ||
                                                                                     diagnostic.Severity == DiagnosticSeverity.Error);

                        foreach (Diagnostic diagnostic in result.Diagnostics)
                        {
                            Logger.Write($"{diagnostic.Id} - {diagnostic.Location}: {diagnostic.GetMessage()}");
                        }

                        return;
                    }
                }

            var appDomainSetup = new AppDomainSetup();

            appDomainSetup.ShadowCopyFiles    = "true";
            appDomainSetup.ApplicationBase    = AppDomain.CurrentDomain.BaseDirectory;
            appDomainSetup.ApplicationName    = "UserCode.dll";
            appDomainSetup.LoaderOptimization = LoaderOptimization.SingleDomain;
            _appDomain = AppDomain.CreateDomain("UserCodeDomain", AppDomain.CurrentDomain.Evidence, appDomainSetup);
            byte[] dllBytes = File.ReadAllBytes("UserCode.dll");
            byte[] pdbBytes = File.ReadAllBytes("UserCode.pdb");

            //_assembly = Assembly.Load(dllBytes, pdbBytes);
            //_assembly = _appDomain.Load(dllBytes, pdbBytes);
            _appDomain.SetData("dllBytes", dllBytes);
            _appDomain.SetData("pdbBytes", pdbBytes);
            _appDomain.DoCallBack(AssemblyWorker.Load);

            UserCodeExecutionContext executionContext = new UserCodeExecutionContext();

            executionContext.CurrentRecord = _Records[0];

            Logger.Write($"Current record time = {executionContext.CurrentRecord.RecordTime}");
            _appDomain.SetData("executionContext", executionContext);
            _appDomain.DoCallBack(AssemblyWorker.Execute);
            executionContext = _appDomain.GetData("executionContext") as UserCodeExecutionContext;
            Logger.Write($"Post-invoke record time = {executionContext.CurrentRecord.RecordTime}");

            return;

            var assemblies = _appDomain.GetAssemblies();

            _assembly = _appDomain.GetAssemblies().First((x) => x.GetName().Name == "UserCode");


            var type   = _assembly.GetType("UserCodeClass");
            var method = type.GetMethod("<Factory>", BindingFlags.Static | BindingFlags.Public);

            Logger.Write($"Current record time = {executionContext.CurrentRecord.RecordTime}");
            method.Invoke(null, new object[] { new object[2] {
                                                   executionContext, null
                                               } });
            Logger.Write($"Post-invoke record time = {executionContext.CurrentRecord.RecordTime}");

            AppDomain.Unload(_appDomain);
            _assembly  = null;
            _appDomain = null;
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            /*
             * try
             * {
             *  ScriptGlobals g = new ScriptGlobals();
             *  foreach (var currentRecord in _Records)
             *  {
             *      g.CurrentRecord = currentRecord;
             *      ScriptState state = script.RunAsync(g).Result;
             *  }
             * }
             * catch (Exception ex)
             * {
             *  Logger.Write($"Exception: {ex.Message}");
             * }
             */
        }