Advertisement
Guest User

Untitled

a guest
Mar 4th, 2020
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.89 KB | None | 0 0
  1. private static Compilation CreateTestCompilation()
  2.     => CSharpCompilation.Create(
  3.         assemblyName: "ConsoleApplication",
  4.         syntaxTrees: new[] {
  5.             SyntaxTreeFromPath(@"Program.cs"),
  6.             SyntaxTreeFromPath(@"InitializerRewriter.cs")
  7.         },
  8.         references: new[] {
  9.             MetadataReferenceFromType<object>(), // mscorlib
  10.             MetadataReferenceFromType<SyntaxTree>(), // CodeAnalysis
  11.             MetadataReferenceFromType<CSharpSyntaxTree>() // CSharpCodeAnalysis
  12.         },
  13.         options: new CSharpCompilationOptions(OutputKind.ConsoleApplication)
  14.     );
  15.  
  16. private static SyntaxTree SyntaxTreeFromPath(string path)
  17.     => CSharpSyntaxTree.ParseText(File.ReadAllText(path)).WithFilePath(path);
  18.  
  19. private static MetadataReference MetadataReferenceFromType<T>()
  20.     => MetadataReference.CreateFromFile(typeof(T).GetTypeInfo().Assembly.Location);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement