static void Main(string[] args)
 {
     try
     {
         if (!CompilationProxy.RunCs(args))
         {
             Environment.ExitCode = 1;
         }
     }
     catch (Exception ex)
     {
         var agg = ex as AggregateException;
         Console.WriteLine("ERROR: An unhandled exception occured");
         if (agg != null)
         {
             agg = agg.Flatten();
             foreach (var inner in agg.InnerExceptions)
             {
                 Console.Error.WriteLine("error: " + inner);
             }
         }
         else
         {
             Console.Error.WriteLine("error: " + ex);
         }
         Environment.ExitCode = 2;
     }
 }
        public static bool RunCs(string[] args)
        {
            var precompilationArgs = PrecompilationCommandLineParser.Parse(args, Directory.GetCurrentDirectory());

            CompilationProxy proxy             = null;
            AppDomain        compilationDomain = null;

            try
            {
                var currentSetup = AppDomain.CurrentDomain.SetupInformation;
                var setup        = new AppDomainSetup()
                {
                    ApplicationName   = currentSetup.ApplicationName,
                    ApplicationBase   = currentSetup.ApplicationBase,
                    ConfigurationFile = precompilationArgs.AppConfig,
                };

                if (setup.ConfigurationFile == null)
                {
                    setup.ConfigurationFile = new[] { "app.config", "web.config" }.Select(x => Path.Combine(precompilationArgs.BaseDirectory, x)).FirstOrDefault(File.Exists);
                    if (!string.IsNullOrWhiteSpace(setup.ConfigurationFile))
                    {
                        Console.WriteLine("WARNING: '" + setup.ConfigurationFile + "' used as fallback config file");
                    }
                }

                compilationDomain = AppDomain.CreateDomain(
                    AppDomainHelper.CsCompilationAppDomainName,
                    AppDomain.CurrentDomain.Evidence,
                    setup);
                compilationDomain.UnhandledException += (s, e) =>
                {
                    Console.WriteLine("error: " + e);
                };

                var assemblyExt = new HashSet <string> {
                    ".dll", ".exe"
                };
                var references = precompilationArgs.References; //.Concat(Directory.EnumerateFiles(AppDomain.CurrentDomain.BaseDirectory).Where(r => assemblyExt.Contains(Path.GetExtension(r)))).ToArray()
                CompilationAssemblyResolver.Register(compilationDomain, references);


                proxy = (CompilationProxy)compilationDomain.CreateInstanceAndUnwrap(
                    typeof(CompilationProxy).Assembly.FullName,
                    typeof(CompilationProxy).FullName);

                Console.CancelKeyPress += (s, e) => proxy?.ForceStop();
                return(proxy.RunCs(precompilationArgs));
            }
            finally
            {
                proxy?.ForceStop();
                // runtime has exited, finish off by unloading the runtime appdomain
                if (compilationDomain != null)
                {
                    AppDomain.Unload(compilationDomain);
                }
            }
        }