Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.CSharp;
  5. using System.CodeDom.Compiler;
  6.  
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. var csc = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } });
  12. var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, "foo.exe", true);
  13. parameters.GenerateExecutable = true;
  14. CompilerResults results = csc.CompileAssemblyFromSource(parameters,
  15. @"using System.Linq;
  16. class Program {
  17. public static void Main(string[] args) {
  18. var q = from i in Enumerable.Range(1,100)
  19. where i % 2 == 0
  20. select i;
  21. }
  22. }");
  23. results.Errors.Cast<CompilerError>().ToList().ForEach(error => Console.WriteLine(error.ErrorText));
  24. }
  25. }
  26.  
  27. public abstract class BaseClass
  28. {
  29. public virtual void Foo1() { }
  30. public virtual bool Foo2() { return false; }
  31. ...
  32. }
  33.  
  34. using System;
  35. using System.Collections.Generic;
  36. using System.IO;
  37. using System.Linq;
  38. using System.Reflection;
  39. using Microsoft.CodeAnalysis;
  40. using Microsoft.CodeAnalysis.CSharp;
  41. using Microsoft.CodeAnalysis.Emit;
  42.  
  43. namespace RoslynCompileSample
  44. {
  45. class Program
  46. {
  47. static void Main(string[] args)
  48. {
  49. SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(@"
  50. using System;
  51.  
  52. namespace RoslynCompileSample
  53. {
  54. public class Writer
  55. {
  56. public void Write(string message)
  57. {
  58. Console.WriteLine(message);
  59. }
  60. }
  61. }");
  62.  
  63. string assemblyName = Path.GetRandomFileName();
  64. MetadataReference[] references = new MetadataReference[]
  65. {
  66. MetadataReference.CreateFromFile(typeof(object).Assembly.Location),
  67. MetadataReference.CreateFromFile(typeof(Enumerable).Assembly.Location)
  68. };
  69.  
  70. CSharpCompilation compilation = CSharpCompilation.Create(
  71. assemblyName,
  72. syntaxTrees: new[] { syntaxTree },
  73. references: references,
  74. options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
  75.  
  76. using (var ms = new MemoryStream())
  77. {
  78. EmitResult result = compilation.Emit(ms);
  79.  
  80. if (!result.Success)
  81. {
  82. IEnumerable<Diagnostic> failures = result.Diagnostics.Where(diagnostic =>
  83. diagnostic.IsWarningAsError ||
  84. diagnostic.Severity == DiagnosticSeverity.Error);
  85.  
  86. foreach (Diagnostic diagnostic in failures)
  87. {
  88. Console.Error.WriteLine("{0}: {1}", diagnostic.Id, diagnostic.GetMessage());
  89. }
  90. }
  91. else
  92. {
  93. ms.Seek(0, SeekOrigin.Begin);
  94. Assembly assembly = Assembly.Load(ms.ToArray());
  95.  
  96. Type type = assembly.GetType("RoslynCompileSample.Writer");
  97. object obj = Activator.CreateInstance(type);
  98. type.InvokeMember("Write",
  99. BindingFlags.Default | BindingFlags.InvokeMethod,
  100. null,
  101. obj,
  102. new object[] { "Hello World" });
  103. }
  104. }
  105.  
  106. Console.ReadLine();
  107. }
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement