Advertisement
Guest User

CodeDom Sample

a guest
Aug 19th, 2014
2,423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.11 KB | None | 0 0
  1. using System;
  2. using System.CodeDom.Compiler;
  3. using System.IO;
  4. using Microsoft.CSharp;
  5.  
  6.  
  7. namespace CodeDomTest
  8. {
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             string source1 = File.ReadAllText(@"Source path here");
  14.             string source2 = File.ReadAllText(@"Source path here");
  15.             var results = CompileCsharpSource(new[] { source1, source2 }, "App.exe");
  16.  
  17.             if (results.Errors.Count == 0)
  18.                 Console.WriteLine("No Errors");
  19.             else
  20.             {
  21.                 foreach (CompilerError error in results.Errors)
  22.                     Console.WriteLine(error.ErrorText);
  23.             }
  24.  
  25.             Console.ReadLine();
  26.         }
  27.  
  28.         private static CompilerResults CompileCsharpSource(string[] sources, string output, params string[] references)
  29.         {
  30.             var parameters = new CompilerParameters(references, output);
  31.             parameters.GenerateExecutable = true;
  32.             using (var provider = new CSharpCodeProvider())
  33.                 return provider.CompileAssemblyFromSource(parameters, sources);
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement