Logo
HOWTO: Know the actual type behind a System.__ComObject type returned by an .Object property

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio .NET 2002
Date: February 2006   Microsoft Visual Studio .NET 2003
Updated: March 2013   Microsoft Visual Studio 2005
      Microsoft Visual Studio 2008
      Microsoft Visual Studio 2010
      Microsoft Visual Studio 2012
Introduction

This article explains how to get the actual type behind the System.__ComObject type returned by the .Object property of several classes of the Visual Studio extensibility model.

More Information

Several classes of the extensibility model provide a property named "Object" of the System.Object type. Among them are the following:

  • EnvDTE.Project.Object
  • EnvTDE.ProjectItem.Object
  • EnvDTE.Window.Object
  • EnvDTE.AddIn.Object
  • EnvDTE.UIHierarchyItem.Object

The type is declared as System.Object because depending on the instance, it can return one type or another. For example, the type returned by a toolwindow when calling Window.Object depends on the kind of toolwindow: while the Solution Explorer toolwindow will return an object implementing the EnvDTE.UIHierarchy interface, the Task List toolwindow will return an object implementing the EnvDTE.TaskList interface. However, when you make a call like this to guess its type:

MessageBox.Show(objWindow.Object.GetType().FullName)

You get "System.__ComObject" as result, which is not very helpful. Instead, to know the actual type behind the COM wrapper, you can use this other statement:

MessageBox.Show(Microsoft.VisualBasic.Information.TypeName(objWindow.Object))

The Information module resides in the Microsoft.VisualBasic namespace, inside the Microsoft.VisualBasic.dll assembly, which is added automatically to VB.NET projects but you will need to add it to your project if you are using C#, at least until your add-in is coded.

Once you know the actual type, you can cast the object to its actual type and work with it.



Go to the 'Visual Studio Extensibility (VSX)' web site for more articles like this (Articles section)


Top