Toodleey
Toodleey

Reputation: 950

Finding members which are enum

I want to do something along the line of this

var membersOfTypeEnum =  typeof(myType).GetMembers().Where(i => i.IsEnum);

however, in the mockup above i is of type MemberInfo and does not implement IsEnum.

GetType(i).Name says RuntimeType which is the reflected type that contains the real type (I think), but I cannot seem to find a way to get to the type of the member itself.

So, how do I find members (that are enums) of a given type?

Edit, lets say I want to reflect this type:

public static class MyType
{
    public enum EnumMember
    {
        One = 1,
        Two = 2,
    }

   public static string NotEnumMember = "this is a string";
}

Upvotes: 3

Views: 168

Answers (2)

Jamiec
Jamiec

Reputation: 136094

This should do what you're asking - use GetNestedTypes:

var membersOfTypeEnum =  typeof(MyType).GetNestedTypes()
                           .Where(i => i.IsEnum);

Live example: http://rextester.com/CGK11010

Upvotes: 4

Ilya Ivanov
Ilya Ivanov

Reputation: 23626

Use GetNestedTypes to get all nested types and them check if it is a Enum

typeof(MyType)
    .GetNestedTypes()
    .Where(type => type.IsEnum)

Upvotes: 8

Related Questions