ahjesus C# Flags 位域略说_.NET_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > .NET > ahjesus C# Flags 位域略说

ahjesus C# Flags 位域略说

 2013/12/6 16:26:06  AhJesus  博客园  我要评论(0)
  • 摘要:classProgram{[Flags]publicenumWeek{[Description("星期一")]Monday=1<<0,//1[Description("星期二")]Tuesday=1<<1,//2[Description("星期三")]Wednesday=1<<2,//4[Description("星期四")]Tursday=1<<3,//8[Description("星期五")]Friday=1<<4
  • 标签:C#
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;
    } 
}

 

发表评论
用户名: 匿名