How to list the possible values of an enum?

So I am inspecting a ToString() call on an enum variable.  How do I get the enumerator list of that enum?

I've gotten as far as getting an ITypeElement and confirming it is an enum type.  But now I'm not sure how to proceed.

public override void VisitInvocationExpression(IInvocationExpression invocationExpressionParam)
{
    IReferenceExpression expression = invocationExpressionParam.InvokedExpression as IReferenceExpression;
    if (expression == null)
        return;

    var rer = expression.Reference as IReferenceExpressionReference;
    if(rer == null)
        return;

    ResolveResult resolveResult = rer.Resolve();

    IDeclaredElement e = resolveResult.DeclaredElement;
    
    if(e == null)
        return;
    
    ITypeElement containingType = e.GetContainingType();
    if(containingType == null)
        return;

    if( containingType.CLRName != "System.Enum" )
        return;

    string[] enumlistnames = ??????
}

Thanks,
Josh

2 comments

var type = TypeFactory.CreateType(typeElement);
var symbolTable = type.GetSymbolTable;

Once you got symbol table for the type it contains all members declared in
as well as inherited members like ToString(). The only thing left is to
filter out enum members from the table. You can do it using Filter maethod.
Declared element are filtered as declaredElement.GetElementType() ==
CLRDeclaredElementType.ENUM_MEMBER.

--
Sergey V. Coox
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


0

Hi Sergey,

Thanks for the help.  I'm not where I want to be yet though. 

I did like you said, and I can see all the members of System.Enum in the symbol table. I can't see the members of my enum type.

For some context, the code I am analyzing looks like below.  I am analyzing the invocation against the variable e within the if statement.  I do not see the members 'First' and 'Second' in the symbol table.

enum MyEnum { First, Second }
void foo(MyEnum e) {
     if(e.ToString() == "First"){
     }
}

I am thinking that the problem might be when I am resolving perhaps?  Should I not be getting an ITypeElement for 'MyEnum' type instead of a 'System.Enum' type?

0

Please sign in to leave a comment.