0

Is there a way to print all the enums in C# class library or namespace? I want the name to be printed with its values.

For example if I have the namespace as below:

namespace MyCompany.SystemLib
{
    public enum ItemStatus
    {
        None = 0,
        Active = 1,
        Inactive = 2    
    }

    public enum MyEnum
    {
        EnumVal1 = 1,
        EnumVal2 = 2,
        EnumVal3 = 3,

    }   
}

I would like to print them delimited as below (to a textfile) Bullet given for clarity here not needed in output.

  • ItemStatus,None=0,Active=1,Inactive=1
  • MyEnum,EnumVal1=1,EnumVal2=2,EnumVal3

I don't know where to start. Any help is appreciated.

Saghir A. Khatri
  • 3,429
  • 6
  • 45
  • 76
stackovrflo
  • 93
  • 3
  • 6
  • Have a look at .NET Reflection mechanisms. This should give you a head start. – Crono May 15 '14 at 17:13
  • You're probably looking for the class System.Enum. It has the methods GetValues and GetNames, which is probably what you need. To find all the enums in your assembly, you will need to use Reflection. – Mephy May 15 '14 at 17:13
  • You can certainly get a list of enums from a given assembly (assuming permissions are right and so on). Is that your question? or are you just after the GetValues and GetNames methods? – Gayot Fow May 15 '14 at 17:13
  • this might help you: http://stackoverflow.com/questions/79693/getting-all-types-in-a-namespace-via-reflection – MaxOvrdrv May 15 '14 at 17:16
  • I appreciate folks helping. For down voters, seriously asking a question gets down votes? – stackovrflo May 15 '14 at 17:17
  • lots of people really want to help newcomers. Others waiting for down voting without any clue to the newbie? I will ignore it for the guys helping out. – stackovrflo May 15 '14 at 18:04

3 Answers3

5

Reflection to the rescue!

List<string> allEnums = new List<string>();
var allTypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(assembly => assembly.GetTypes());

foreach (Type type in allTypes)
{
    if (type.Namespace == "MyCompany.SystemLib" && type.IsEnum)
    {
        string enumLine = type.Name + ",";
        foreach (var enumValue in Enum.GetValues(type))
        {
            enumLine += enumValue + "=" + ((int)enumValue).ToString() + ",";
        }

        allEnums.Add(enumLine);
    }
}

The first line goes over all assemblies currently loaded in memory (because a namespace can be scattered over many DLLs and EXEs) and filters out those in the right namespace, and that are enums. This can be streamlined into a LINQ query if you'd like.

The inner loop goes over the values of the enum, and uses GetName to match the string to the value.

Avner Shahar-Kashtan
  • 14,492
  • 3
  • 37
  • 63
1

Try using the Enum.GetNames() method.

Andrew
  • 4,953
  • 15
  • 40
  • 58
0

It can be done using LINQ and Reflection ofcourse.

var asm = Assembly.LoadFrom("path of the assembly");
var enums = asm.GetTypes().Where(x => x.IsEnum).ToList();

var result = enums
            .Select(
                x =>
                    string.Format("{0},{1}", x.Name,
                        string.Join(",",Enum.GetNames(x)
                        .Select(e => string.Format("{0}={1}", e, (int)Enum.Parse(x, e))))));

File.WriteAllLines("path", result);
Selman Genç
  • 100,147
  • 13
  • 119
  • 184