How to get the list of Enum properties of a class?

.net, c#, enums, properties, reflection

Solution

You are almost there. Just use

if (property.PropertyType.IsEnum)
    // Celebrate

In .NET 4.5, you might need to get a TypeInfo object from the property type.

Problem

Closely related to How to get the list of properties of a class?, I've gotten as far as that question goes but I'm interested in knowing which of the returned properties are enumerations. My first (unlikely) guess was along the lines of: ``` foo A; foreach (var property in A.GetType().GetProperties()) { if (property.PropertyType is Enum) //Celebrate } ``` This did not work. It's valid, but Visual Studio was even able to warn in advance that "The given expression is never of the provided ('System.Enum') type". To my understanding, C# Enums are wrappers over top of primitive counting types (defaulting with int, but also possibly byte, short, etc). I can easily test to see of the properties are of these types, but that will lead me to a lot of false positives in my search for Enums.

Original source

Related problems