Ver versión en español    
 
Home
10 Reasons to use MZ-Tools
MZ-Tools 6.0 for VS.NET
Editions
Features
Online Documentation
MZ-Tools SDK
Download
Purchase
Version History (RSS)  
FAQ & Support
MZ-Tools 3.0 for VB6 & VBA
Features
Online Documentation
Download (freeware)
Donations (Paypal)
Version History (RSS)  
FAQ & Support
User Reviews
Community Place
Contact  
For Add-In Developers
About
   
 
User Testimonials

I'm an avid supporter of MZ-Tools. It's a product I couldn't do without and your level of support is outstanding.

Jan Hyde (Visual Basic MVP)

You will soon wonder how you ever lived without it.

Andy Maggs

More user reviews
 
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
      Microsoft Visual Studio 2005
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 back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this


MZ-Tools 6.0 for Visual Studio .NET

You can code, design, locate code and document your apps much faster using VB.NET, C#, C++ or Visual J#:

Buy MZ-Tools Now Download MZ-Tools Demo

   Top