/// <summary> /// Add a new value (or values) to this JsonSchema's enum list. This JsonSchema (with the /// new value(s)) is then returned so that additional changes can be chained together. /// </summary> /// <param name="enumValue"></param> /// <param name="extraEnumValues"></param> /// <returns></returns> public JsonSchema AddEnum(string enumValue, params string[] extraEnumValues) { if (Enum == null) { Enum = new List <string>(); } if (Enum.Contains(enumValue)) { throw new ArgumentException("enumValue (" + enumValue + ") already exists in the list of allowed values.", "enumValue"); } Enum.Add(enumValue); if (extraEnumValues != null && extraEnumValues.Length > 0) { foreach (string extraEnumValue in extraEnumValues) { if (Enum.Contains(extraEnumValue)) { throw new ArgumentException("extraEnumValue (" + extraEnumValue + ") already exists in the list of allowed values.", "extraEnumValues"); } Enum.Add(extraEnumValue); } } return(this); }
public static T Set <T>(this Enum type, T value, bool state) { if (state) { return(type.Add(value)); } else { return(type.Remove(value)); } }
/// <summary> /// Adds or removes a bit flag or set of bit flags in an enumeration value and returns the new value. /// </summary> /// <typeparam name="T">The type of enumeration.</typeparam> /// <param name="value">The value on which to operate.</param> /// <param name="flag">The flag(s) to add to or remove from <paramref name="value"/>.</param> /// <param name="add">Whether to add or remove the flags; <c>true</c> adds the flags; <c>false</c> removes them.</param> /// <returns>The new value.</returns> public static T AddOrRemove <T>(this Enum value, T flag, bool add) { if (add) { return(value.Add(flag)); } else { return(value.Remove(flag)); } }
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { switch (_itemsType) { case ItemsType.Enum: Enum result = Enum.ToObject(_enumType, 0) as Enum; foreach (Enum item in ListBox.SelectedItems) { result = result.Add(item); } SelectedItems = result; SelectionBoxItems = result.ToString(); break; default: throw new NotImplementedException(); } }
/// <summary> /// Sets flag state on enum. /// Please note that enums are value types so you need to handle the RETURNED value from this method. /// Example: myEnumVariable = myEnumVariable.SetFlag(CustomEnumType.Value1, true); /// </summary> public static T Set <T>(this Enum type, T enumFlag, bool value) { return(value ? type.Add(enumFlag) : type.Remove(enumFlag)); }
//toggles a value public static T Toggle <T>(this Enum type, T value) { return(type.Has(value) ? type.Remove(value) : type.Add(value)); }
public static T Combine <T>(this Enum source, Enum other, bool add) { return((add) ? source.Add <T>(other) : source.Remove <T>(other)); }