Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging dynamically compiled code with Roslyn

Tags:

c#

roslyn

I'm using Roslyn to dynamically compile arbitrary *.cs files. I am able to generate the DLLs and PDBs and execute methods within the loaded assemblies via reflection, but when attaching the VS debugger from System.Diagnostics.Debugger.Launch(), I'm getting 'source not available' errors. The 'Modules' windows in VS shows that the symbols have been loaded successfully. The source in question lives in a project under the same solution, but is not compiled until runtime.

Has anyone successfully gotten this working? Thanks!

reproduction: https://gist.github.com/toraora/a9d4eb8679383fe659da04d3be5c2d6e (I'll put up the actual solution when I finish setting up SSH keys on this machine)

like image 792
Nathan Wong Avatar asked Jun 13 '16 21:06

Nathan Wong


1 Answers

Ah, so the solution was to have:

CSharpSyntaxTree.ParseText(File.ReadAllText(srcfile), path: srcfile, encoding: System.Text.Encoding.UTF8)

Thanks @hvd!

like image 110
Nathan Wong Avatar answered Sep 18 '22 23:09

Nathan Wong