Search code examples
c#vb.netenumsiteration

C# or VB.NET - Iterate all Public Enums


We have a common component in our source which contains all the enums (approx 300!) for a very large application.

Is there any way, using either C# or VB.NET, to iterate through all of them in order to perform an action on each one?

The question How to iterate all “public string” properties in a .net class is almost relevant but the enums I am dealing with are a mix of types


Solution

  • Something along these lines?

    var query = Assembly.GetExecutingAssembly()
                        .GetTypes()
                        .Where(t => t.IsEnum && t.IsPublic);
    
    foreach (Type t in query)
    {
        Console.WriteLine(t);
    }