6

I've a audit class that recovery everything by reflection. I need in my actual point know if an specific property is an Enum, but I'm getting a strange behavior:

During foreach iteration q.PropertyType.IsEnum return false. And using Quick watcher the property is really false, and the IsClass too. So this is basically nothing :)

Studying a little more about the problem I found that Nullable Enum returns false in IsEnum. How can I ignore this nullable and verify if the property is an enum or not?

Custodio
  • 8,594
  • 15
  • 80
  • 115

3 Answers3

8

IsEnum will return false when your property is of a nullable type. In this case, calling Nullable.GetUnderlyingType on q.PropertyType will return the type you want. Then you can check with IsEnum.

Fernando
  • 4,029
  • 1
  • 26
  • 35
2

Edit: I've tried your enum, and it is fetchable. A call to Foo.GetEnumProperties returns an array with "TestProp" in it:

    public enum MyEnum
    {
        [XmlEnumAttribute("Twenty and Something")]
        TwentyTree = 1,
        [XmlEnumAttribute("Thirty and Something")]
        Thirdyfour
    }

    class Foo
    {
        public MyEnum TestProp { get; set; }


        /// <summary>
        /// Get a list of properties that are enum types 
        /// </summary> 
        /// <returns>Enum property names</returns>
        public static string[] GetEnumProperties()
        {
            MemberInfo[] members = typeof(Foo).FindMembers(MemberTypes.Property, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance, null, null);
            List<string> retList = new List<string>();
            foreach (MemberInfo nextMember in members)
            {
                PropertyInfo nextProp = nextMember as PropertyInfo;
                if (nextProp.PropertyType.IsEnum)
                    retList.Add(nextProp.Name);
            } return retList.ToArray();
        }
    }

To do what you are trying to do, I use System.ComponentModel.DescriptionAttribute, then you can fetch it like this:

/// <summary>
/// Get the description for the enum
/// </summary>
/// <param name="value">Value to check</param>
/// <returns>The description</returns>
public static string GetDescription(object value)
{
    Type type = value.GetType();
    string name = Enum.GetName(type, value);
    if (name != null)
    {
        FieldInfo field = type.GetField(name);
        if (field != null)
        {
            DescriptionAttribute attr = Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute)) as DescriptionAttribute;
            if (attr != null)
            {
                string desc = attr.Description;
                return desc;
            }
        }
    }
    return value.ToString();
}
Ed Bayiates
  • 11,060
  • 4
  • 43
  • 62
  • The question aims discover what's wrong with MyEnum declaration. – Custodio May 17 '11 at 20:03
  • 1
    There is nothing wrong with your enum declaration. You are just going about getting the attribute in a way that won't work. – Ed Bayiates May 17 '11 at 20:05
  • Nice. So, to adapt to my question what's the better way to return a bool value saying if a Property is Enum or not – Custodio May 17 '11 at 20:13
  • You mean you have a class foo, and the class has properties, and you want to check its properties to see which ones are enum types? – Ed Bayiates May 17 '11 at 20:19
  • I modified my answer in case that's what you wanted. – Ed Bayiates May 17 '11 at 20:25
  • Yes. allProperties mentioned in question represent everything of a generic object that my audit class receive. So I want to recognize the enum type – Custodio May 17 '11 at 20:25
  • OK. Let me know if this latest code answers your question. Thanks. – Ed Bayiates May 17 '11 at 20:27
  • I did the first part already. The difference is that you have used `q.GetType().IsEnum`, I'm using `q.PropertyType.IsEnum`. I'm testing to verify if it works. – Custodio May 17 '11 at 20:30
  • Oops, sorry, it was supposed to be nextProp.PropertyType.IsEnum. This works fine for me on my own classes. Not sure why it would not work for yours. Have you tried changing from XmlEnumAttribute to DescriptionAttribute? – Ed Bayiates May 17 '11 at 20:40
  • Well, this is possible. But nonviable right now, every enum in whole system use XmlAttribute – Custodio May 17 '11 at 20:42
  • I tried the code on your enum, and it works too. I've edited my answer again. – Ed Bayiates May 17 '11 at 20:51
  • @AvesAvatar! I guess I found! my enum is declared as nullable. – Custodio May 17 '11 at 20:55
  • The enum you had in your original question was not nullable. – Ed Bayiates May 17 '11 at 21:53
  • I don't put the part the code where I declare the Enum, but the problem was nullable. After use skeet solution everything works. – Custodio May 18 '11 at 01:56
0

The general problem with not recognized Enums is that they can be Nullable and then the IsEnum doesn't work.
This is the case here, and using a @Skeet answer Checking if Type instance is a nullable enum in C# I solve my problem.

Community
  • 1
  • 1
Custodio
  • 8,594
  • 15
  • 80
  • 115