Changeset 15009


Ignore:
Timestamp:
05/31/17 13:29:27 (3 months ago)
Author:
pfleck
Message:

#2724 Moved EnumHelper (for SetFlags).

Location:
trunk/sources
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Data.Views/3.3/EnumValueView.cs

    r14597 r15009  
    4343    }
    4444
     45    static EnumValueView() {
     46      if (!typeof(T).IsEnum)
     47        throw new InvalidOperationException("Generic type " + typeof(T).Name + " is not an enum.");
     48    }
     49
    4550    public EnumValueView() {
    4651      InitializeComponent();
    47       this.Name = typeof(T).Name + "EnumView";
     52      this.Caption = typeof(T).Name + "EnumView";
    4853
    4954      valueComboBox.DataSource = Enum.GetValues(typeof(T));
     
    121126    }
    122127  }
     128
     129  internal static class EnumHelper {
     130    //https://stackoverflow.com/a/21581418
     131    public static T SetFlag<T>(this Enum value, T flag, bool set) {
     132      var baseType = Enum.GetUnderlyingType(value.GetType());
     133      dynamic valueAsBase = Convert.ChangeType(value, baseType);
     134      dynamic flagAsBase = Convert.ChangeType(flag, baseType);
     135      if (set)
     136        valueAsBase |= flagAsBase;
     137      else
     138        valueAsBase &= ~flagAsBase;
     139      return (T)valueAsBase;
     140    }
     141  }
    123142}
  • trunk/sources/HeuristicLab.Data.Views/3.3/HeuristicLab.Data.Views-3.3.csproj

    r13425 r15009  
    105105  </PropertyGroup>
    106106  <ItemGroup>
     107    <Reference Include="Microsoft.CSharp" />
    107108    <Reference Include="System" />
    108109    <Reference Include="System.Core">
  • trunk/sources/HeuristicLab.Data/3.3/EnumValue.cs

    r14597 r15009  
    6262    }
    6363  }
    64 
    65   public static class EnumHelper {
    66     public static T SetFlag<T>(this Enum value, T flag, bool set) {
    67       var baseType = Enum.GetUnderlyingType(value.GetType());
    68       dynamic valueAsBase = Convert.ChangeType(value, baseType);
    69       dynamic flagAsBase = Convert.ChangeType(flag, baseType);
    70       if (set)
    71         valueAsBase |= flagAsBase;
    72       else
    73         valueAsBase &= ~flagAsBase;
    74       return (T)valueAsBase;
    75     }
    76   }
    7764}
  • trunk/sources/HeuristicLab.Data/3.3/HeuristicLab.Data-3.3.csproj

    r14597 r15009  
    108108  </PropertyGroup>
    109109  <ItemGroup>
    110     <Reference Include="Microsoft.CSharp" />
    111110    <Reference Include="System" />
    112111    <Reference Include="System.Core">
Note: See TracChangeset for help on using the changeset viewer.