Switch on Enum (with Flags attribute) without declaring every possible combination?

后端 未结 9 2209
忘掉有多难
忘掉有多难 2021-01-31 14:15

how do i switch on an enum which have the flags attribute set (or more precisely is used for bit operations) ?

I want to be able to hit all cases in a switch that matche

9条回答
  •  感动是毒
    2021-01-31 14:58

    Cast it to its base type, the great thing about this is it tells you when there are duplicate values present.

    [Flags]
    public enum BuildingBlocks_Property_Reflection_Filters
    {
        None=0,
        Default=2,
        BackingField=4,
        StringAssignment=8,
        Base=16,
        External=32,
        List=64,
        Local=128,
    }
    
    switch ((int)incomingFilter)
    {
        case (int)PropFilter.Default:
            break;
        case (int)PropFilter.BackingField:
            break;
        case (int)PropFilter.StringAssignment:
            break;
        case (int)PropFilter.Base:
            break;
        case (int)PropFilter.External:
            break;
        case (int)PropFilter.List:
            break;
        case (int)PropFilter.Local:
            break;
        case (int)(PropFilter.Local | PropFilter.Default):
            break;
    
    }
    

提交回复
热议问题