第一件事情,我不知道该怎么标题这个问题 – 我甚至困惑如何说出来.
现在的问题.我们来看一下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; }