Logo
BUG: EnvDTE.CodeElement.Name doesn't return interface name for explictly implemented C# interface events

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

Introduction

When you have a C# interface that with an event member and a class that implements the interface explicitly (using the interface name in the implemented members), the EnvDTE.CodeElement.Name property doesn't return the name of the interface for the event as it does for methods and properties.

More Information

Steps to reproduce the problem:

  • Create a class with this code:
Language: C#   Copy Code Copy Code (IE only)
namespace ClassLibrary1
{
   public interface Interface1
   {
      event System.EventHandler Event1;
      void Method1();
      int Property1
      {
         get;
      }
   }
   public class Class1 : Interface1
   {
      void Interface1.Method1()
      {
      }
      event System.EventHandler Interface1.Event1
      {
         add
         {
         }
         remove
         {
         }
      }
      int Interface1.Property1
      {
         get
         {
            return 1;
         }
      }
   }
}
  • 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

Sub ShowCodeElements(ByVal codeElements As EnvDTE.CodeElements)

   For Each codeElement As EnvDTE.CodeElement In codeElements

      Select Case codeElement.Kind

         Case vsCMElement.vsCMElementNamespace

            ShowCodeElements(DirectCast(codeElement, CodeNamespace).Members)

         Case vsCMElement.vsCMElementClass

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

         Case vsCMElement.vsCMElementEvent, vsCMElement.vsCMElementFunction, vsCMElement.vsCMElementProperty

            MessageBox.Show(codeElement.Name)

      End Select

   Next

End Sub

The macro returns the names "Interface1.Method1" and "Interface1.Property1" but it returns "Event1" instead of "Interface1.Event1".


Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this


Top