Add a watch without a full namespace in Visual Studio

I am adding a code snippet to view a window from code at runtime, copying it from code and adding it to the viewport. If the code contains methods such as Convert.ToString (), XMLDocument.Load (), File.Write (), etc. The default .NET namespaces such as System.IO, System.Xml etc., then it gives an error: The name "Convert" does not exist in the current context ", but it does not give an error when I add the full namespace to the added part of the code in the viewer, for example: System.Convert.ToString (123) gives the correct value.My program code does not have a full namespace before any .NET framework method called in it, because if I I add a namespace before calling the method, so what to use adding a namespace on top of each file, using the keyword β€œusing.” If I add a namespace above the code to the file, then I don’t need to add the full namespace to this file code before calling the method. What can I do except add the full namespace in the viewer every time I copy the code to the watch list so that the watch does not give an error See the screenshot below: enter image description here

Update: I am adding a method to the watch list to check the result of the method before it is executed. It guarantees that the method will not give any errors or exceptions during execution, and I can edit the code because this method was not actually executed when the program code was executed. I put a breakpoint on this method that calls the code, and first add this code to the viewport to check if there is any error, because if I do not, I will have to run the whole program again to fix the next time.

+6
source share
1 answer

The debugger uses a context in which the current instruction pointer (a small yellow arrow to the left of the source window) attempts to evaluate the function.

So, if the line of code where you left off has "using System"; at the top of the file, you can enter Convert.ToInt32 (123) in the viewport. If you are in another file that does not have this, you will need to fully name it.

I tried this with the following test case:

// Main.cs using System; namespace TestCon { class Program { static void Main(string[] args) { Foo foo = new Foo(); Console.WriteLine(Convert.ToString(123)); Console.WriteLine(Convert.ToInt32("234")); } } } //Foo.cs (note that there are no using statements in this file) namespace TestCon { class Foo { public Foo() { } } } 

If I go to any point in the main.cs file, I can copy the Convert expressions to the viewport without the System namespace qualifier, and they will evaluate. If I find (or run to a breakpoint) in my Foo () constructor, I get the error message "The name" Convert "does not exist in the current context" unless I add a namespace classifier to it.>

Note. Even when the expression can be evaluated, you often have to remove the refresh button (the two arrows in the circle to the right of the time zone window), because the debugger cannot determine if the call to the CLR is triggered causes side effects.

Hope this helps.

+1
source

Source: https://habr.com/ru/post/948733/


All Articles