none
How to get a list of properties of an Enum RRS feed

  • Question

  • Hi all,

    the value of an enum label can be called with Enumname.labelname. This call behaves like a public static property. My question: Is it possible to get a list of the labels using **reflection**? Using method GetProperties() delievers an empty list.

    TIA.


    Regards Jörg



    Sunday, June 19, 2016 10:43 PM

Answers

  • Hi Jörg Debus,

    >>"Using method GetProperties() delievers an empty list."

    Use GetFields instead of GetProperties. The labels are impement by fields.

    public enum Ball
    {
        Basketball,
        Football,
        Pingpong
    }
    Type type = typeof(Ball);
    FieldInfo[] fields = type.GetFields();
    for (int i = 0; i < fields.Length; i++)
    {
        Console.WriteLine(fields[i].Name + ", Is public:" + fields[i].IsPublic);
    }

    In addition, we also could use Enum.GetValues method to get all the lables of a enum type.

    var values = Enum.GetValues(typeof(Ball));
    foreach (var value in values)
    {
        Console.WriteLine(value);
    }

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, June 20, 2016 1:08 AM
    Moderator

All replies

  • Hi Jörg Debus,

    >>"Using method GetProperties() delievers an empty list."

    Use GetFields instead of GetProperties. The labels are impement by fields.

    public enum Ball
    {
        Basketball,
        Football,
        Pingpong
    }
    Type type = typeof(Ball);
    FieldInfo[] fields = type.GetFields();
    for (int i = 0; i < fields.Length; i++)
    {
        Console.WriteLine(fields[i].Name + ", Is public:" + fields[i].IsPublic);
    }

    In addition, we also could use Enum.GetValues method to get all the lables of a enum type.

    var values = Enum.GetValues(typeof(Ball));
    foreach (var value in values)
    {
        Console.WriteLine(value);
    }

    Best Regards,
    Li Wang


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Monday, June 20, 2016 1:08 AM
    Moderator
  • Li,

    thanks a lot for this valuable hint.Now I'm enabled to look after my coding problem, whci is simply to specify a struct which contains an enum and exposes the enum labels as public static fields.

    public struct StructWithEnum : INullable, IBinarySerialize { private Ball Flags;
    private int32 Action; // some code to implement nullability and serialization [Flags] public enum Ball : byte { Basketball = 1, Football = 2, Tennis = 4 } }

    What I want is, that this struct can be used as follows: if ((byte)MyEnumStruct & StructWithEnum.Football != 0) { ... }

    This is no problem when implemented manually with fields like this:
    public static readonly byte Football = StructWithEnum.Football;

    This is a little uncool if we take maintenance into account. But how can it be implemented dynamically using the internal fields from the Ball enum?


    Regards Jörg



    Monday, June 20, 2016 9:27 PM