public void ReadMeTypeSafeWrappers() { // Enum.GetUnderlyingType(typeof(T)) Type underlyingType = EnumUtil.GetUnderlyingType <YourEnum>(); // Enum.GetName(typeof(T), value) string nameFoo = EnumUtil.GetName(YourEnum.Foo); // (T)Enum.Parse(typeof(T), value) YourEnum valueFoo = EnumUtil.Parse <YourEnum>("Foo"); // (T)Enum.Parse(typeof(T), value, ignoreCase) YourEnum valueFoo2 = EnumUtil.Parse <YourEnum>("foo", ignoreCase: true); // (T[])typeof(T).GetEnumValues() YourEnum[] values = EnumUtil.GetValues <YourEnum>(); // typeof(T).GetEnumNames() string[] names = EnumUtil.GetNames <YourEnum>(); // Enum.TryParse(value, out result) YourEnum possibleValue; bool success = EnumUtil.TryParse("foob", out possibleValue); // Enum.TryParse(value, ignoreCase, out result) YourEnum possibleValue2; bool sucess2 = EnumUtil.TryParse("foo", true, out possibleValue2); // Enum.IsDefined(typeof(T), name) bool isDef2 = EnumUtil.IsDefined <YourEnum>("Foo"); }
public void ReadMeBitwiseOperations() { // a | b YourEnum fooOrBar = EnumUtil.BitwiseOr(YourEnum.Foo, YourEnum.Bar); // a & b YourEnum fooAndBar = EnumUtil.BitwiseAnd(YourEnum.Foo, YourEnum.Bar); // a ^ b YourEnum fooXorBar = EnumUtil.BitwiseExclusiveOr(YourEnum.Foo, YourEnum.Bar); // ~ a YourEnum notBar = EnumUtil.BitwiseNot(YourEnum.Bar); }
public void ReadMeConvFromANumberType() { // Conversion from a numeric type to an enum type YourEnum val0 = EnumUtil.FromByte <YourEnum>(2); YourEnum val1 = EnumUtil.FromSByte <YourEnum>(2); YourEnum val2 = EnumUtil.FromInt16 <YourEnum>(2); YourEnum val3 = EnumUtil.FromUInt16 <YourEnum>(2); YourEnum val4 = EnumUtil.FromInt32 <YourEnum>(2); YourEnum val5 = EnumUtil.FromUInt32 <YourEnum>(2); YourEnum val6 = EnumUtil.FromInt64 <YourEnum>(2L); YourEnum val7 = EnumUtil.FromUInt64 <YourEnum>(2UL); YourEnum val8 = EnumUtil.FromSingle <YourEnum>(2f); YourEnum val9 = EnumUtil.FromDouble <YourEnum>(2.0); }
public void ReadMeFlagAttributeOperations() { // Has Flag? bool hasFlag = EnumUtil.HasFlag(YourEnum.Foo | YourEnum.Bar, YourEnum.Bar); // Set Flag YourEnum barSet = EnumUtil.SetFlag(default(YourEnum), YourEnum.Bar); // Unset (Remove) Flag YourEnum barUnset = EnumUtil.UnsetFlag(barSet, YourEnum.Bar); // Toggle Flag On / Off YourEnum fooSet = EnumUtil.ToggleFlag(barUnset, YourEnum.Foo); // Toggle Flag based on a passed in boolean YourEnum fooUnset = EnumUtil.ToggleFlag(fooSet, YourEnum.Foo, false); // Checks whether the FlagsAttribute it defined on the Enum // Note: Toggle, Set, Unset, and HasFlag functions do not ensure that // FlagsAttribute is defined bool hasFlagsShortcut = EnumUtil.HasFlagsAttribute <YourEnum>(); }
public SampleClass(YourEnum attributeValue) : this() { MyAttributeProperty = attributeValue; }