visual-studiovsx

How can I detect attributes on enum values in VB, using the FileCodeModel


In my Visual Studio Package I use a recursive scan of the CodeElements in the FileCodeModel. In particular, I want to detect all attributes in a code file.

I have found that attributes associated with an enum value are represented by CodeElements if they are defined in C#, but not if they are defined in VB.

Example in C#:

[Description("Enumeration test")]
enum testEnum
{
  [Description("Number one")]
  one,
  [Description("Number two")]
  two
}

Example in VB:

<Description("Enumeration test")>
Enum testEnum
  <Description("Number one")>
  One
  <Description("Number two")>
  Two
End Enum

In both cases the Description attribute associated with the enum itself is represented in the FileCodeModel.

The Description attributes associated with the enum values are only included in the FileCodeModel for the C# version.

Does anybody know a way to make these attributes show up in the FileCodeModel?

I think that this might have worked in the past and been broken, but I'm not 100% certain about that.

I can provide sample code if that would help, but I would want to make a clean example program, rather than just copying the code from my project.

Note: I have already asked more or less the same question on a Microsoft forum about two weeks ago, so in a sense this is a duplicate question. The only reply to that question suggested using the CodeModel instead of the FileCodeModel, but my experiments indicate that the CodeModel has the same problem.


Solution

  • I have reproduced the problem, it's a bug in Roslyn. The EnvDTE file code model is based on Roslyn since Visual Studio 2015. Likely this worked until Visual Studio 2013.

    I have already filed a bug on GitHub with the exact location of the bug:

    EnvDTE.CodeVariable.Attributes returns empty collection for VB.NET enum members (it works for C# enum members)

    Until fixed, as workaround, if your extension targets only VS 2015 and higher you can avoid EnvDTE and work directly against Roslyn.