HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Getting all public constants from the main and nested classes

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
themainallpublicgettingnestedclassesandfromconstants

Problem

I wrote this recursive function to get all constants from a class and its subclasses. Can it be simplified?

private static IEnumerable GetPublicConstants(Type type)
{
    var subtypes = type.GetNestedTypes(BindingFlags.Public);

    foreach (var subtype in subtypes)
    {
        foreach (var constant in GetPublicConstants(subtype))
        {
            yield return constant;
        }
    }

    var publicStaticFields = type.GetFields(BindingFlags.Public |
         BindingFlags.Static | BindingFlags.FlattenHierarchy)
         .Where(fi => fi.IsLiteral && !fi.IsInitOnly);

    foreach (var fieldInfo in publicStaticFields)
    {
        yield return fieldInfo;
    }
}


For example, it would return 6 constants for the following class:

public static class MyClass
{
    public const string AAA = "";

    public static class A
    {
        public const string Aaaaa = "";

        public static class AB
        {
            public const string ABaaa = "";
        }
    }

    public static class B
    {
        public const string Baaa = "";
        public const string Bbbb = "";
    }

    public static class C
    {
        public const string Caaa = "";
    }
}


What I don't like is the fact I'm having 2 loops where I yield return 1 item because of inability to yield return a list of items.

Solution

How about this:

private static IEnumerable GetPublicConstants(Type type)
{
    return GetPublicClassConstants(type)
        .Concat(type.GetNestedTypes(BindingFlags.Public).SelectMany(GetPublicConstants));
}

private static IEnumerable GetPublicClassConstants(Type type)
{
    return type
        .GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
        .Where(fi => fi.IsLiteral && !fi.IsInitOnly);
}


OR, as I do enjoy playing PGA Pro Code Golf:

private static IEnumerable GetPublicConstants(Type type)
{
    return type
        .GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
        .Where(fi => fi.IsLiteral && !fi.IsInitOnly)
        .Concat(type.GetNestedTypes(BindingFlags.Public).SelectMany(GetPublicConstants));
}

Code Snippets

private static IEnumerable<FieldInfo> GetPublicConstants(Type type)
{
    return GetPublicClassConstants(type)
        .Concat(type.GetNestedTypes(BindingFlags.Public).SelectMany(GetPublicConstants));
}

private static IEnumerable<FieldInfo> GetPublicClassConstants(Type type)
{
    return type
        .GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
        .Where(fi => fi.IsLiteral && !fi.IsInitOnly);
}
private static IEnumerable<FieldInfo> GetPublicConstants(Type type)
{
    return type
        .GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy)
        .Where(fi => fi.IsLiteral && !fi.IsInitOnly)
        .Concat(type.GetNestedTypes(BindingFlags.Public).SelectMany(GetPublicConstants));
}

Context

StackExchange Code Review Q#101385, answer score: 7

Revisions (0)

No revisions yet.