使用反射和 PropertyInfo 无法识别我的枚举

发布于 2024-11-08 16:28:16 字数 279 浏览 0 评论 0 原文

我有一个审计课程,可以通过反思恢复一切。在我的实际观点中,我需要知道特定属性是否是枚举,但我得到了一个奇怪的行为:

在 foreach 迭代期间 q.PropertyType.IsEnum 返回 false。使用 Quick watcher 属性确实是 false,IsClass 也是如此。所以这基本上没什么:)

进一步研究这个问题,我发现 Nullable Enum 在 IsEnum 中返回 false。我怎样才能忽略这个可为空的并验证该属性是否是枚举?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑咖 2024-11-15 16:28:16

当您的属性为可为空类型时,IsEnum 将返回 false。在这种情况下,在 Nullable.GetUnderlyingType >q.PropertyType 将返回您想要的类型。然后你可以使用 IsEnum 检查。

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.

梦回梦里 2024-11-15 16:28:16

编辑:我已经尝试过你的枚举,它是可以获取的。对 Foo.GetEnumProperties 的调用返回一个包含“TestProp”的数组:

    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();
        }
    }

为了执行您想要执行的操作,我使用 System.ComponentModel.DescriptionAttribute,然后您可以像这样获取它:

/// <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();
}

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();
}
獨角戲 2024-11-15 16:28:16

无法识别的枚举的一般问题是它们可以为 Nullable,然后 IsEnum 不起作用。
这里就是这种情况,并使用 @Skeet 答案 检查 Type 实例是否是 C# 中可为 null 的枚举 我解决了我的问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文