Getting a list of constants using Reflection

Thanks to Drew Marsh I was able to create a function to get a list of the constants for a particular type in c#.  There were two key things I was missing:
BindingFlags.FlattenHierarchy
Specifies that static members up the hierarchy should be returned. Static members include fields, methods, events, and properties. Nested types are not returned (More on BindingFlags)
IsInitOnly property of a FieldInfo
Gets a value indicating whether the field can only be set in the body of the constructor.(More Info)

Anyway here is the function:

/// <SUMMARY>
/// This method will return all the constants from a particular
/// type including the constants from all the base types
/// </SUMMARY>
/// <PARAM NAME="TYPE">type to get the constants for</PARAM>
/// <RETURNS>array of FieldInfos for all the constants</RETURNS>
private FieldInfo[] GetConstants(System.Type type)
{
    ArrayList constants = new ArrayList();

    FieldInfo[] fieldInfos = type.GetFields(
        // Gets all public and static fields

        BindingFlags.Public | BindingFlags.Static | 
        // This tells it to get the fields from all base types as well

        BindingFlags.FlattenHierarchy);

    // Go through the list and only pick out the constants
    foreach(FieldInfo fi in fieldInfos)
        // IsLiteral determines if its value is written at 
        //   compile time and not changeable
        // IsInitOnly determine if the field can be set 
        //   in the body of the constructor
        // for C# a field which is readonly keyword would have both true 
        //   but a const field would have only IsLiteral equal to true
        if(fi.IsLiteral && !fi.IsInitOnly)
            constants.Add(fi);           

    // Return an array of FieldInfos
    return (FieldInfo[])constants.ToArray(typeof(FieldInfo));
}
PS: Code colorized by a codehtmler that Shawn A Van Ness and I are working, you will see more information about that soon.
Published Thursday, February 20, 2003 1:05 PM by puzzlehacker

Comments

# re: Getting a list of constants using Reflection

I was hoping to access the values of the constants. Do you now how to do this?

Wednesday, May 14, 2003 1:21 AM by Clayton

# re: Getting a list of constants using Reflection

Yes in the for loop above you can just say fi.GetValue(null); and it will return the value of that constant as an object.

HTH
Wes

Wednesday, May 14, 2003 4:35 AM by Wes Haggard

# re: Getting a list of constants using Reflection

When using fi.GetValue(null), I just get the constant name? Any ideas?

Friday, January 16, 2004 4:13 PM by mick

# re: Getting a list of constants using Reflection

You rock, This helped me out a lot.

Monday, September 21, 2009 4:45 PM by Dennis Webb

# re: Getting a list of constants using Reflection

Are there any issues with making this a static method in a static class?

Tuesday, December 1, 2009 9:52 AM by mkamoski

# re: Getting a list of constants using Reflection

Do you have an answer for this question... "When using fi.GetValue(null), I just get the constant name? Any ideas?"???

Tuesday, December 1, 2009 11:34 AM by mkamoski

# re: Getting a list of constants using Reflection

Check out this version of the same code (lazy evaluated)

private IEnumerable<FieldInfo> GetConstants(System.Type type) {

return type.GetFields( BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy).Where(x=>x.IsLiteral && !x.IsInitOnly);

}

Friday, April 16, 2010 10:20 AM by Norman Harebottle III