/// <summary> /// Constructor. /// </summary> /// <param name="prms"><see cref="PreSandboxParams"/> object to /// use to determine what to output. Cannot be <c>null</c>.</param> public CLConsole(PreSandboxParams prms) { System.Diagnostics.Debug.Assert(prms != null); // Set streams according to params. this.Verbose = !prms.silent && prms.verbose ? Console.Out : TextWriter.Null; this.Out = !prms.silent ? Console.Out : TextWriter.Null; this.Error = Console.Error; this.Debug = prms.debug ? Console.Out : TextWriter.Null; }
/// <summary> /// Runs the ClpArgAsm registration/unregistration tasks according to the /// given program arguments. /// </summary> /// <param name="args">Program arguments.</param> /// <returns>Program exit code. 0 if everything went well.</returns> public static int Run(string[] args) { // Assume everything will work. int exitCode = 0; // First parse pre-sandbox arguments to have access to parameters // needed before anything else. PreSandboxParams preSandboxPrms = new PreSandboxParams(); CommandLineArguments.Parser.ParseArguments(args, preSandboxPrms, msg => { }); // Create console to use in this method to output according to params. CLConsole console = new CLConsole(preSandboxPrms); // Show copyright information except if user told us not to. if (!preSandboxPrms.nologo) { ShowCopyright(console); } try { // Now parse all parameters and check if user wants help. If parameter issues // are encountered, they will be displayed here. Params prms = new Params(); bool wantsHelp = CommandLineArguments.Parser.ParseHelp(args); bool validParams = CommandLineArguments.Parser.ParseArguments(args, prms); if (!wantsHelp && validParams) { // Create a new AppDomain to perform registration/unregistration tasks, // so that if something goes horribly wrong in the assembly we'll be loading, // we won't be affected by it. Set the ApplicationBase to the current directory // from where we've been invoked, since it might contain dependencies. AppDomain domain = AppDomain.CreateDomain("CLRegAsm.Core", null, new AppDomainSetup { ApplicationBase = Environment.CurrentDirectory }); // Create an instance of this class to execute in the new AppDomain. Core sandboxedCore = (Core)domain.CreateInstanceFromAndUnwrap( Assembly.GetExecutingAssembly().CodeBase, typeof(Core).FullName); if (sandboxedCore == null) { throw new CoreException(String.Format("Type \"{0}\" not found when trying to " + "create it in sandboxed AppDomain", typeof(Core).FullName)); } // Execute in sandboxed AppDomain. exitCode = sandboxedCore.RunSandboxed(args, prms); } else { // Display usage information. if (!validParams) { console.WriteLine(); } ShowUsage(console); } } catch (Exception e) { // An error occured. Print it to stderr, then return // a non-zero error code to indicate the error. console.PrintException(e); exitCode = 1; } return(exitCode); }