“Just My Code” Debugging

You might remember from Chapter 2 and Chapter 3, “The Anatomy of a Visual Basic Project,” that every time you create a Visual Basic application the IDE generates some background code. Moreover, your code often invokes system code that you do not necessarily need to investigate. In Visual Basic the IDE offers the capability of debugging just your own code, excluding system and auto-generated code. This feature is also known as Just My Code debugging. This is useful because you can focus on your code. Just My Code is enabled by default in Visual Studio 2015. To disable it or enable it, open the Options window, select the Debugging node on the left, and then flag or unflag the Enable Just My Code (Managed Only) check box, as shown in Figure 5.1.

Image

FIGURE 5.1 Enabling/disabling Just My Code debugging.

Behind the scenes, Just My Code adds (or removes) some .NET attributes to auto-generated code that can influence the debugger behavior. To see a simple example, open or create a project and then click the Show All Files button in Solution Explorer. After doing this, go to the Settings.designer.vb code file. Listing 5.2 shows the content of the My namespace definition inside the file.

LISTING 5.2 Understanding Just My Code Behind the Scenes


Namespace My
     <Global.Microsoft.VisualBasic.HideModuleNameAttribute(), _
      Global.System.Diagnostics.DebuggerNonUserCodeAttribute(), _
      Global.System.Runtime.CompilerServices.CompilerGeneratedAttribute()> _
     Friend Module MySettingsProperty
         <Global.System.ComponentModel.
          Design.HelpKeywordAttribute("My.Settings")> _
         Friend ReadOnly Property Settings() As
        Global.DebuggingFeatures.My.MySettings
             Get
                Return Global.DebuggingFeatures.My.MySettings.Default
             End Get
         End Property
     End Module
End Namespace


You can notice that the module MySettingsProperty is decorated with a particular attribute named System.Diagnostics.DebuggerNonUserCodeAttribute. This attribute indicates to the debugger that the code is not your code (user code) and that it will not be debugged when Just My Code is on. Three attributes influence the debugger’s behavior in this feature. Table 5.1 shows the complete list.

Image

TABLE 5.1 Just My Code Attributes

Of course, you can use attributes of your own so that you can influence the behavior of the debugger when Just My Code is disabled.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.23.147