c# - How to debug code compiled with Roslyn in a Visual Studio extension inside current Visual Studio host? -
i have visual studio extensions use roslyn project in current opened solution, compile , run methods it. project can modified programmer.
i have compiled project in visual studio extension current visualstudioworkspace.
private static assembly compileandload(compilation compilation) { using (memorystream dllstream = new memorystream()) using (memorystream pdbstream = new memorystream()) { emitresult result = compilation.emit(dllstream, pdbstream); if (!result.success) { ienumerable<diagnostic> failures = result.diagnostics.where(diagnostic => diagnostic.iswarningaserror || diagnostic.severity == diagnosticseverity.error); string failuresexception = "failed compile code generation project : \r\n"; foreach (diagnostic diagnostic in failures) { failuresexception += $"{diagnostic.id} : {diagnostic.getmessage()}\r\n"; } throw new exception(failuresexception); } else { dllstream.seek(0, seekorigin.begin); return appdomain.currentdomain.load(dllstream.toarray(), pdbstream.toarray()); } } } then can load assembly in current domain, it's types , invoke methods.
the problem need allow programmer put breakpoints if current configuration of loaded solution debug.
i need run code in current visual studio host extension , allow debugged in current visual studio instance.
you have attach debugger running visual studio host. need to:
- get hold of dte object,
- find (current) process in dte.debugger.localprocesses
- attach debugger - example
- run method compiled assembly
you'd want separate command/button this, don't switch on current build configuration. that's how else (e.g. test runners , visual studio). also, load compiled assembly in new appdomain, otherwise it'll stick around for ever.
there's more "creative" solution of injecting system.diagnostics.debugger.launch() call method you're run (modify correct compilation.syntaxtrees before calling emit) - seriously, don't this.
Comments
Post a Comment