博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ahjesus C# Flags 位域略说
阅读量:6234 次
发布时间:2019-06-21

本文共 2912 字,大约阅读时间需要 9 分钟。

class Program {    [Flags]    public enum Week {        [Description("星期一")]        Monday = 1 << 0,//1        [Description("星期二")]        Tuesday = 1 << 1,//2        [Description("星期三")]        Wednesday = 1 << 2,//4        [Description("星期四")]        Tursday = 1 << 3,//8        [Description("星期五")]        Friday = 1 << 4,//16        [Description("星期六")]        Saturday = 1 << 5,//32        [Description("星期日")]        Sunday = 1 << 6//64    }    static void Main(string[] args) {        //1.|表示两边求并集(元素相加,相同元素只出现一次)        Week week = Week.Wednesday | Week.Monday | Week.Monday | Week.Tuesday;//Monday,Tuesday,Wednesday                     //2.&表示两边是否其中一个是另外一个的子集,如果是返回子集,否则返回0        //week = Week.Monday & week;//Monday        //week = (Week.Sunday | Week.Monday | Week.Tuesday) & week;//Monday,Tuesday        //week = Week.Sunday & week;//0        //出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢!        //3.^表示从两者的并集中去除两者的交集        //week = Week.Monday ^ Week.Monday;//0        //week = (Week.Monday | Week.Tuesday) ^ (Week.Tuesday | Week.Wednesday);//Monday,Wednesday        //4.~表示取补,按二进制每4位一组,不足4位最左边补0,然后1变0,0变1,用法主要和&一起使用去除其中的某个元素        //week = ~Week.Wednesday;//-5        //week = week & (~Week.Monday); //Monday,Tuesday        //存入数据库可以转换为int,从数据库中读出来可以转换为枚举        //week = Week.Monday  | Week.Tuesday;        //int i = (int)week;//3        //week = (Week)Enum.Parse(typeof(Week), "3");//Monday,Tuesday        NameValueCollection nvc = GetNVCFromEnumValue(typeof(Week));        foreach (string key in nvc.AllKeys)        {            Console.WriteLine(key + ":" + nvc[key]);        }                    Console.ReadKey();    }    /// < summary>      /// 从枚举类型和它的特性读出并返回一个键值对      /// < /summary>      /// < param name="enumType">Type,该参数的格式为typeof(需要读的枚举类型)< /param>      /// < returns>键值对< /returns>      public static NameValueCollection GetNVCFromEnumValue(Type enumType) {        NameValueCollection nvc = new NameValueCollection();        Type typeDescription = typeof(DescriptionAttribute);        System.Reflection.FieldInfo[] fields = enumType.GetFields();        string strText = string.Empty;        string strValue = string.Empty;        foreach (FieldInfo field in fields) {            if (field.FieldType.IsEnum) {                strValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)).ToString();                object[] arr = field.GetCustomAttributes(typeDescription, true);                if (arr.Length > 0) {
//出自http://www.cnblogs.com/ahjesus 尊重作者辛苦劳动成果,转载请注明出处,谢谢! DescriptionAttribute aa = (DescriptionAttribute)arr[0]; strText = aa.Description; } else { strText = field.Name; } nvc.Add(strText, strValue); } } return nvc; } }

 

转载于:https://www.cnblogs.com/ahjesus/p/3461427.html

你可能感兴趣的文章
基于http协议使用protobuf进行前后端交互
查看>>
bash腳本編程之三 条件判断及算数运算
查看>>
php cookie
查看>>
linux下redis安装
查看>>
弃 Java 而使用 Kotlin 的你后悔了吗?| kotlin将会是最好的开发语言
查看>>
JavaScript 数据类型
查看>>
量子通信和大数据最有市场突破前景
查看>>
对‘初学者应该选择哪种编程语言’的回答——计算机达人成长之路(38)
查看>>
如何申请开通微信多客服功能
查看>>
Sr_C++_Engineer_(LBS_Engine@Global Map Dept.)
查看>>
非监督学习算法:异常检测
查看>>
jquery的checkbox,radio,select等方法总结
查看>>
Linux coredump
查看>>
Ubuntu 10.04安装水晶(Mercury)无线网卡驱动
查看>>
我的友情链接
查看>>
nginx在reload时候报错invalid PID number
查看>>
ElasticSearch 2 (32) - 信息聚合系列之范围限定
查看>>
VS2010远程调试C#程序
查看>>
[MicroPython]TurniBit开发板DIY自动窗帘模拟系统
查看>>
Python3.4 12306 2015年3月验证码识别
查看>>