C# Flags 位域略说

/*
 * 由SharpDevelop创建。
 * 用户: Changweihua
 * 日期: 2013/12/6
 * 时间: 22:32
 *
 * changweihua@outlook.com
 * http://www.cmono.net
 *
 */
using System;
using System.Reflection;
using System.ComponentModel;
using System.Collections.Specialized;
namespace Sample2
{
    [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
    }
         
    class Program
    {
        public 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
                 
            //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) {
                        DescriptionAttribute aa = (DescriptionAttribute)arr[0];
                        strText = aa.Description;
                    }
                    else {
                        strText = field.Name;
                    }
                    nvc.Add(strText, strValue);
                }
            }
            return nvc;
        }
             
    }
}


知识共享许可协议
《C# Flags 位域略说》常伟华 创作。
采用 知识共享 署名-相同方式共享 3.0 中国大陆 许可协议进行许可。
相邻依据:发表时间
  • 多说评论
  • 签名
  • 新浪微博
  • 默认评论
  • Tab Header 5

0 条评论 / 点击此处发表评论

Tab Content 5

开发技术


开发平台和工具

sitemap     169.92ms