• ahjesus C# Flags 位域略说


    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;
        } 
    }
  • 相关阅读:
    强迫症患者
    GG的匹配串
    漂洋过海来看你
    Fire or Retreat
    1011. A+B和C (15)
    1010. 一元多项式求导 (25)
    1009. 说反话 (20)
    1008. 数组元素循环右移问题 (20)
    1007. 素数对猜想 (20)
    1006. 换个格式输出整数 (15)
  • 原文地址:https://www.cnblogs.com/ahjesus/p/3461427.html
Copyright © 2020-2023  润新知