Logo
BUG: C# indexers are not mapped to EnvDTE80.CodeProperty2 in Visual Studio macros or add-ins

Author: Carlos J. Quintero (Microsoft MVP) Applies to: Microsoft Visual Studio 2005
Date: July 2009   Microsoft Visual Studio 2008

Introduction

Visual Studio 2005 introduced an extended automation model in the assembly EnvDTE80.dll that provides new classes and methods not existing in EnvDTE.dll. See: INFO: Assemblies used in Visual Studio Extensibility.

When you have an indexer in C# and you use the file code model to discover the code elements, the indexer is mapped to an EnvDTE.CodePreperty code element, when it should be more suitable to be mapped to EnvDTE80.CodeProperty2 to get access to its parameters collection (the Parameters property provided by EnvDTE80.CodeProperty2 but not by EnvDTE.CodeProperty).

More Information

Steps to reproduce the problem:

  • Create a class with this code:
Language: C#   Copy Code Copy Code (IE only)
namespace Namespace1
{
   public class Class1
   {
      private string[] data = new string[5];

      public string this[int index]
      {
         get
         {
            return data[index];
         }
         set
         {
            data[index] = value;
         }
      }
   }
}
  • Create and execute this macro:
Language: VB.NET   Copy Code Copy Code (IE only)
Sub Macro1()

   Try

      ShowCodeElements(DTE.ActiveDocument.ProjectItem.FileCodeModel.CodeElements)

   Catch ex As Exception
      MsgBox(ex.ToString)
   End Try

End Sub

Private Sub ShowCodeElements(ByVal codeElements As EnvDTE.CodeElements)

   For Each codeElement As EnvDTE.CodeElement In codeElements

      If TypeOf codeElement Is EnvDTE.CodeNamespace Then

         ShowCodeElements(DirectCast(codeElement, EnvDTE.CodeNamespace).Members)

      ElseIf TypeOf codeElement Is EnvDTE.CodeType Then

         ShowCodeElements(DirectCast(codeElement, EnvDTE.CodeType).Members)

      ElseIf TypeOf codeElement Is EnvDTE80.CodeProperty2 Then

         MsgBox("Found EnvDTE80.CodeProperty2: " & codeElement.Name)

      ElseIf TypeOf codeElement Is EnvDTE.CodeProperty Then

         MsgBox("Found EnvDTE.CodeProperty: " & codeElement.Name)

      End If

   Next

End Sub   

This is a bug of Visual Studio 2005 and 2008. It is fixed in Visual Studio 2010.



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