Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Reflection;
  7. using System.Net;
  8. using Microsoft.CSharp;
  9. using System.CodeDom.Compiler;
  10.  
  11. namespace ConsoleApplication2
  12. {
  13. class Program
  14. {
  15. static void Main(string[] args)
  16. {
  17. string source =
  18. @"
  19. namespace Foo
  20. {
  21. public class Bar
  22. {
  23. public void SayHello()
  24. {
  25. System.Console.WriteLine(""Hello World"");
  26. }
  27. }
  28. }
  29. ";
  30.  
  31. Dictionary<string, string> providerOptions = new Dictionary<string, string>
  32. {
  33. {"CompilerVersion", "v3.5"}
  34. };
  35. CSharpCodeProvider provider = new CSharpCodeProvider(providerOptions);
  36.  
  37. CompilerParameters compilerParams = new CompilerParameters
  38. {GenerateInMemory = true,
  39. GenerateExecutable = false};
  40.  
  41. CompilerResults results = provider.CompileAssemblyFromSource(compilerParams, source);
  42.  
  43. if (results.Errors.Count != 0)
  44. throw new Exception("Mission failed!");
  45.  
  46. object o = results.CompiledAssembly.CreateInstance("Foo.Bar");
  47. MethodInfo mi = o.GetType().GetMethod("SayHello");
  48. mi.Invoke(o, null);
  49. }
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement