Question

I'm trying to generate a typescript interfaces class complete with proper enums.

I'm stuck on how to get the value of the enum member.

Enum.Members gets CodeElements, not CodeProperties and if I try and cast, it breaks. It also breaks with CodeEnum.

What I'm trying to do is get the value so that it's explicit with value1 = 1, value2= 2 etc. instead of just value1, value2 because the enums may not be 0 based.

Any help would be greatly appreciated.

Was it helpful?

Solution

"CodeElements" sounds as if you are using the EnvDTE interfaces to gain access to your enumerations. If so, all enum items are in the Members-Property of the EnvDTE.CodeEnum type. They are of type EnvDTE.CodeVariable and the explicit value is stored in the InitExpression of the Variable.

Have a look at this Enum:

    // C# Enum to reflect 
    public enum MyEnum { Hi = 10, There = 15 }

And this T4 CodeSnippet reflecting the Enum:

    <#
      // the enumeration object you already seem to have
      EnvDTE.CodeEnum theEnum;

      // iterate all enumeration items
      foreach(EnvDTE.CodeVariable variable in theEnum.Members)
      {
          // render name and value
          #><#= variable.Prototype #> = <#= variable.InitExpression.ToString() #>
    <#}
    #>

Should result in:

    Hi = 10
    There = 15

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top