private CompilerResults DoCompile() { CompilerParameters parameters = new CompilerParameters(); parameters.GenerateExecutable = false; parameters.GenerateInMemory = true; parameters.IncludeDebugInformation = false; parameters.ReferencedAssemblies.AddRange(SelectedAssemblies.Select(a => a.Location).ToArray()); var unit = CreateCompilationUnit(); var writer = new StringWriter(); codeProvider.GenerateCodeFromCompileUnit( unit, writer, new CodeGeneratorOptions() { BracingStyle = "C", ElseOnClosing = true, IndentString = " ", }); CompilationUnitCode = writer.ToString(); return(codeProvider.CompileAssemblyFromDom(parameters, unit)); }
/// <summary> /// Expand macro statements into C# code /// </summary> /// <returns></returns> bool CompileMacro() { if (UiVsRoot == null) { return(ErrorMsg("UI Automation not available")); } var csharp = new StringBuilder(); foreach (var line in MacroLines) { if (QuitWhenDone) { return(ErrorMsg("No code allowed after #quit")); } if (line is CodeLine) { var codeLine = line as CodeLine; csharp.Append(codeLine.Code + "\r\n"); continue; } if (!GenerateStatement(line as Statement, csharp)) { return(false); } } if (csharp.Length > 0) { CSharpMethodCode = csharp.ToString(); } AutoRun = string.IsNullOrEmpty(Name); if (AutoRun) { Name = "Macro_" + Path.GetRandomFileName().Replace(".", ""); } else if (!SaveMacro(Name)) { return(ErrorMsg("Macro already defined")); } foreach (var sv in ServiceRefs.Values.Where(x => string.IsNullOrEmpty(x.Type))) { sv.Type = sv.Interface; } var selectedAssemblyNames = SelectedAssemblies .Select(x => new AssemblyName(x)) .GroupBy(x => x.FullName) .Select(x => x.First()); var allAssemblies = AppDomain.CurrentDomain.GetAssemblies() .GroupBy(x => x.GetName().Name) .ToDictionary(x => x.Key, x => x.AsEnumerable(), StringComparer.InvariantCultureIgnoreCase); var refAssemblies = selectedAssemblyNames .GroupBy(x => allAssemblies.ContainsKey(x.Name)) .SelectMany(x => x.Key ? x.SelectMany(y => allAssemblies[y.Name]) : x.Select(y => { try { return(Assembly.Load(y)); } catch { return(null); } })); RefAssemblies = refAssemblies .Where(x => x != null) .Select(x => x.Location); return(NoError()); }