The main VS extension for running a fragment and displaying results (similar to a clock)

I would like to develop an extension for Visual Studio that runs a small piece of code at runtime after a breakpoint has been removed.

To add some clarity, I really want to name some code in the same way as I do, manually writing it in the immediate window and displaying the results well (preferably a tree view).

I did not want to publish this question, since it seems rather broad at first glance, but I am sure that there are not many ways to do this.

I really studied Roslyn, but I find compilation-time only if I'm not mistaken?

I have the code to do this already, and it works fine in the direct window, but I want to add it as a function, as it is a bit cumbersome to manually enter it, and it is also not easy to execute it. what I need is somewhere between the โ€œclockโ€ and the โ€œimmediateโ€ function.

Question

What platform / technology allows me to extend Visual Studio so that at runtime I can run ad hoc snippets and display the results well?

+5
source share
2 answers

I suggest exploring this:

0
source

Q: What platform / technology allows me to extend Visual Studio so that at runtime I can run ad hoc snippets and display the results well?

I would approach this as follows:

Basically you create a function as follows:

 // Add some convenient using's. public class EvaluationClass { public static object Evaluate() { return /* paste the evaluated expression here */; } } 

The good thing about this method is that the results are automatically boxed / cast to object , which is great. The only problem here is that you can do a second compilation run for the void case, which will fail because it cannot be placed in the / casted box for the object.

Further

  1. Use csc.exe to compile the dll. Handle the errors accordingly. CSC is located in your .NET platform folder ( c:\windows\microsoft.net\framework[x]\... )
  2. Use reflection to load the DLL and execute the method in the extension.

The main trick is as follows:

 try { var assembly = Assembly.Load(tempDllName); // TODO: check if assembly is null --> compilation error var eval = assembly.GetType("EvaluationClass").GetMethod("Evaluate"); // TODO: check if eval is null --> compilation error var result = eval.Invoke(); // Draw result on window } catch (Exception ex) { // Handle exception appropriately } 
  1. Improve this ... the most useful thing here is to grab the top stack stack from the debugger and pass the this object as value to the method ... I have not considered how to do this; it should be available in the API somewhere though ...
0
source

All Articles