/// <summary> /// Instantiates a new coordinator which will generate an assembly of the /// specified name in the specified location on disk. /// </summary> public CodeDomAssemblyCoordinator(string outputDir, string assemblyName) { this.AssemblyName = assemblyName; this.OutputDirectory = outputDir; this.internalNamespace = new CodeDomNamespace(RootNamespace); this.internalNamespace.References.Add(NamespaceReference.System); this.models = new List <CodeDomClassModel>(); }
/// <summary> /// Compile this class into a single-class assembly. Mostly useful for unit testing. /// See <see cref="CodeDomAssemblyCoordinator"/> for more typical compilation. /// </summary> public bool CompileClassFromGeneratedCsFile(CodeDomNamespace namesp) { if (!this.isReadyToCompile) { this.GenerateCCsharpClass(namesp); } return(CompilationUtilities.CompileAssemblyFromFiles( this.filePath, namesp.Name, new[] { this.OutputFilePath }, namesp)); }
/// <summary> /// Writes this class out to a .cs file. /// </summary> public bool GenerateCCsharpClass(CodeDomNamespace namesp) { // Generate the code with the C# code provider. CSharpCodeProvider provider = new CSharpCodeProvider(); CodeCompileUnit compileUnit = new CodeCompileUnit(); // Add the application models to the root namespace and stage for compilation CodeNamespace newNamepsace = namesp.ConvertToCodeNamespace(); newNamepsace.Types.Add(this.targetClass); compileUnit.Namespaces.Add(newNamepsace); string sourceFile; if (provider.FileExtension[0] == '.') { sourceFile = $"{this.ClassName}{provider.FileExtension}"; } else { sourceFile = $"{this.ClassName}.{provider.FileExtension}"; } this.OutputFilePath = Path.Combine(this.filePath, sourceFile); // Create a TextWriter to a StreamWriter to the output file. using (StreamWriter sw = new StreamWriter(this.OutputFilePath, false)) { IndentedTextWriter tw = new IndentedTextWriter(sw, " "); // Generate source code using the code provider. provider.GenerateCodeFromCompileUnit(compileUnit, tw, new CodeGeneratorOptions()); tw.Close(); } this.isReadyToCompile = true; return(true); }
public static bool CompileAssemblyFromFiles(string outputPath, string outputAssemblyName, string[] files, CodeDomNamespace namesp) { CSharpCodeProvider provider = new CSharpCodeProvider(); // Build the parameters for source compilation. CompilerParameters cp = new CompilerParameters(); // Add an assembly reference. cp.ReferencedAssemblies.AddRange(namesp.GetAssemblyReferences()); // Set the assembly file name to generate. cp.OutputAssembly = Path.Combine(outputPath, $"{outputAssemblyName}.dll"); // Save the assembly as a physical file. cp.GenerateInMemory = false; // Invoke compilation. CompilerResults cr = provider.CompileAssemblyFromFile(cp, files); if (cr.Errors.Count > 0) { // Display compilation errors. Console.WriteLine("Errors building {0} into {1}", cp.OutputAssembly, cr.PathToAssembly); foreach (CompilerError ce in cr.Errors) { Console.WriteLine(" {0}", ce.ToString()); Console.WriteLine(); } } else { Console.WriteLine("Source {0} built into {1} successfully.", cp.OutputAssembly, cr.PathToAssembly); } // Return the results of compilation. if (cr.Errors.Count > 0) { return(false); } return(true); }