Free cookie consent management tool by TermsFeed Policy Generator

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

#2724 Moved EnumHelper (for SetFlags).

Location:
trunk/sources/HeuristicLab.Data.Views/3.3
Files:
2 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">
Note: See TracChangeset for help on using the changeset viewer.