locked
Type.IsEnum() and Type.GetFields()? RRS feed

Answers

  • When you add the System.Reflection namespace to your code, then the GetTypeInfo() extension method becomes visible.

    Then you can:

    bool isEnum = type.GetTypeInfo().IsEnum;
    var fields = type.GetTypeInfo().DeclaredFields;

    But for fetching enum field names I prefer this one:

    var fields = Enum.GetNames(type);

    • Marked as answer by roschler Friday, January 24, 2014 10:54 AM
    Friday, January 24, 2014 6:41 AM

All replies

  • When you add the System.Reflection namespace to your code, then the GetTypeInfo() extension method becomes visible.

    Then you can:

    bool isEnum = type.GetTypeInfo().IsEnum;
    var fields = type.GetTypeInfo().DeclaredFields;

    But for fetching enum field names I prefer this one:

    var fields = Enum.GetNames(type);

    • Marked as answer by roschler Friday, January 24, 2014 10:54 AM
    Friday, January 24, 2014 6:41 AM
  • Thanks Diederik!

    -- roschler

    Friday, January 24, 2014 10:54 AM