TestExecWindow
Class TestExecWin_VS2022Package - Visual Studio integration, enabling DTE access

Overview:

Visual Studio integration by deriving from base class 'AsyncPackage'

Base class "AsyncPackage" derives from a couple of IAsyncXX interfaces which care for proper integration with Visual Studio. To make it even more simple it is recommended to use the Extensibility Template Pack 2022 by Mads Kristensen. Then you can derive from base class ToolkitPackage which is provided by the extension package and itself derives from AsyncPackage. For a code sample see next section.

Getting acess to DTE

Microsoft Visual Studio allows programmatic access via a special environment, the "DTE" (= development tools environment). The "DTE object" is the root of the automation model for Visual Studio.

To get access to DTE object you can establish a connection within the package class of your C# assembly. The reference to the DTE object can be distributed within your application classes as needed.

public sealed class TestExecWin_VS2022Package : ToolkitPackage
{
protected override async Task InitializeAsync(CancellationToken cancellationToken,
IProgress<ServiceProgressData> progress)
{
await this.RegisterCommandsAsync();
this.RegisterToolWindows(); // => calls MyToolWindow constructor
// Remark: In this specific project some object references are stored within the global struct
// MyGlobals for easy access from any point of code.
// Allow access to package (e.g. for getting options page which is managed by package)
MyGlobals.myPackage = this;
// Establish connection to automation object DTE of Visual Studio
if (MyGlobals.myDTE == null)
{
await JoinableTaskFactory.SwitchToMainThreadAsync();
var dte = await this.GetServiceAsync(typeof(EnvDTE.DTE)) as EnvDTE80.DTE2;
Microsoft.Assumes.Present(dte);
// Pass DTE reference to static data members of other classes
MyGlobals.myDTE = dte;
VisualStudioConnector.dte = dte;
}
}
}