加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 有没有办法将类中的枚举属性设置为所有可用的枚举?

发布时间:2020-12-15 17:45:15 所属栏目:百科 来源:网络整理
导读:第一件事情,我不知道该怎么标题这个问题 – 我甚至困惑如何说出来. 现在的问题.我们来看一下System.IO.FileSystemWatcher类,你设置它的NotifyFilter属性: this.FileSystemWatcher1.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime |
第一件事情,我不知道该怎么标题这个问题 – 我甚至困惑如何说出来.

现在的问题.我们来看一下System.IO.FileSystemWatcher类,你设置它的NotifyFilter属性:

this.FileSystemWatcher1.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.FileName 
            | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security 
            | NotifyFilters.Size;

这是一个相当多的代码设置一个属性.检查NotifyFilter,这是一个枚举.是否有一个“懒惰”或“快捷方式”方法来一次设置所有这些属性?我知道这不一定是必需的,但我的好奇心是激动人心的.

this.FileSystemWatcher1.NotifyFilter =< NotifyFilters.All> ?

解决方法

你可以随时做这样的事情,
NotifyFilter ret = 0;
foreach(NotifyFilter v in Enum.GetValues(typeof(NotifyFilter)))
{
    ret |= v;   
}

我不知道一个更好的方法,不幸的是.但是你总是可以把它放在通用的实用方法中.

private static T GetAll<T>() where T : struct,IConvertible
{
    if (!typeof(T).IsEnum)
    {
       throw new NotSupportedException(); // You'd want something better here,of course.
    }

    long ret = 0; // you could determine the type with reflection,but it might be easier just to use multiple methods,depending on how often you tend to use it.
    foreach(long v in Enum.GetValues(typeof(T)))
    {
        ret |= v;
    }

    return (T)ret;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读