枚举上最常见的C#按位操作 [英] Most common C# bitwise operations on enums

查看:184
本文介绍了枚举上最常见的C#按位操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的生活,我不记得如何设置,删除,切换或测试一点位。要么我不确定,要么混合,因为我很少需要这些。所以bit-cheat-sheet会很好。



例如:

  flags = flags | FlagsEnum.Bit4; //设置位4. 

  if((flags& FlagsEnum.Bit4))== FlagsEnum.Bit4)//是否有更少的冗长的方式?你可以给出所有其他常用操作的例子,最好用C#语法使用[Flags]枚举。 ?

解决方案

我对这些扩展做了更多的工作 - 你可以在这里找到代码



我写了一些扩展名扩展System.Enum的方法,我经常使用...我不是声称他们是防弹的,但他们已经帮助... 评论删除...

 命名空间Enum.Extensions {

public static class EnumerationExtensions {

public static bool has< T>这个System.Enum类型,T值){
try {
return(((int)(object)type&(int)(object)value)==(int)(object)value) ;
}
catch {
return false;
}
}

public static bool Is< T>(此System.Enum类型,T值){
try {
return(int) (object)type ==(int)(object)value;
}
catch {
return false;
}
}


public static T Add< T>(此System.Enum类型,T值){
try {
return(T)(object)(((int)(object)type |(int)(object)value));
}
catch(Exception ex){
throw new ArgumentException(
string.Format(
无法从枚举类型{0}追加值。 ,
typeof(T).Name
),ex);
}
}


public static T删除< T>(此System.Enum类型,T值){
try {
return(T)(object)(((int)(object)type&〜(int)(object)value));
}
catch(Exception ex){
throw new ArgumentException(
string.Format(
无法从枚举类型{0}中删除值。 ,
typeof(T).Name
),ex);
}
}

}
}

然后它们被使用如下:

  SomeType value = SomeType.Grapes; 
bool isGrapes = value.Is(SomeType.Grapes); // true
bool hasGrapes = value.Has(SomeType.Grapes); // true

value = value.Add(SomeType.Oranges);
value = value.Add(SomeType.Apples);
value = value.Remove(SomeType.Grapes);

bool hasOranges = value.Has(SomeType.Oranges); // true
bool isApples = value.Is(SomeType.Apples); // false
bool hasGrapes = value.Has(SomeType.Grapes); // false


For the life of me, I can't remember how to set, delete, toggle or test a bit in a bitfield. Either I'm unsure or I mix them up because I rarely need these. So a "bit-cheat-sheet" would be nice to have.

For example:

flags = flags | FlagsEnum.Bit4;  // Set bit 4.

or

if ((flags & FlagsEnum.Bit4)) == FlagsEnum.Bit4) // Is there a less verbose way?

Can you give examples of all the other common operations, preferably in C# syntax using a [Flags] enum?

解决方案

I did some more work on these extensions - You can find the code here

I wrote some extension methods that extend System.Enum that I use often... I'm not claiming that they are bulletproof, but they have helped... Comments removed...

namespace Enum.Extensions {

    public static class EnumerationExtensions {

        public static bool Has<T>(this System.Enum type, T value) {
            try {
                return (((int)(object)type & (int)(object)value) == (int)(object)value);
            } 
            catch {
                return false;
            }
        }

        public static bool Is<T>(this System.Enum type, T value) {
            try {
                return (int)(object)type == (int)(object)value;
            }
            catch {
                return false;
            }    
        }


        public static T Add<T>(this System.Enum type, T value) {
            try {
                return (T)(object)(((int)(object)type | (int)(object)value));
            }
            catch(Exception ex) {
                throw new ArgumentException(
                    string.Format(
                        "Could not append value from enumerated type '{0}'.",
                        typeof(T).Name
                        ), ex);
            }    
        }


        public static T Remove<T>(this System.Enum type, T value) {
            try {
                return (T)(object)(((int)(object)type & ~(int)(object)value));
            }
            catch (Exception ex) {
                throw new ArgumentException(
                    string.Format(
                        "Could not remove value from enumerated type '{0}'.",
                        typeof(T).Name
                        ), ex);
            }  
        }

    }
}

Then they are used like the following

SomeType value = SomeType.Grapes;
bool isGrapes = value.Is(SomeType.Grapes); //true
bool hasGrapes = value.Has(SomeType.Grapes); //true

value = value.Add(SomeType.Oranges);
value = value.Add(SomeType.Apples);
value = value.Remove(SomeType.Grapes);

bool hasOranges = value.Has(SomeType.Oranges); //true
bool isApples = value.Is(SomeType.Apples); //false
bool hasGrapes = value.Has(SomeType.Grapes); //false

这篇关于枚举上最常见的C#按位操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆