发现如果枚举有"旗帜与QUOT;属性集 [英] Finding out if an enum has the "Flags" attribute set

查看:97
本文介绍了发现如果枚举有"旗帜与QUOT;属性集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用反射,我该如何确定一个枚举是否具有标志属性与否



所以MyColor返回true

  [国旗] 
公共枚举MyColor
{
黄色= 1,
绿= 2,
红= 4,
蓝= 8
}

和MyTrade返回false

 公共枚举MyTrade 
{
股= 1,
楼= 2​​,
网= 4,
}


解决方案

如果您是在.NET 4.5:(typeof运算(MyColor).GetCustomAttributes<的FlagsAttribute>()任何())

 如果
{
}


Using reflection, how do I determine whether an enum has the Flags attribute or not

so for MyColor return true

[Flags]
public enum MyColor
{
    Yellow = 1,
    Green = 2,
    Red = 4,
    Blue = 8
}

and for MyTrade return false

public enum MyTrade
{
    Stock = 1,
    Floor = 2,
    Net = 4,
}

解决方案

If you are on .NET 4.5:

if (typeof(MyColor).GetCustomAttributes<FlagsAttribute>().Any())
{
}

这篇关于发现如果枚举有&QUOT;旗帜与QUOT;属性集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆