static void Main(string[] args) { EnumNoFlags enumNoFlags001 = EnumNoFlags.Blank; EnumNoFlags enumNoFlagsUnion = EnumNoFlags.Blank | EnumNoFlags.Blue; EnumNoFlags enumNoFlags002 = enumNoFlags001 | enumNoFlagsUnion; EnumNoFlags enumNoFlags003 = enumNoFlags001 & enumNoFlagsUnion; EnumWithFlags enumWithFlags001 = EnumWithFlags.Blank; EnumWithFlags enumWithFlagsUnion = EnumWithFlags.Blank | EnumWithFlags.Blue; EnumWithFlags enumWithFlags002 = enumWithFlags001 | enumWithFlagsUnion; EnumWithFlags enumWithFlags003 = enumWithFlags001 & enumWithFlagsUnion; foreach (var item in Enum.GetNames(typeof(EnumNoFlags))) { Console.WriteLine(item); } foreach (int item in Enum.GetValues(typeof(EnumNoFlags))) { Console.WriteLine(item); } foreach (byte item in Enum.GetValues(typeof(EnumWithType))) { Console.WriteLine(item); } var a = enumNoFlags001.GetType().Name + "_" + Enum.GetName(typeof(EnumWithType), 1); }
public void TestAsFlagsToEnum() { int options; EnumNoFlags noFlags = EnumNoFlags.Default; EnumWithFlags withFlags = EnumWithFlags.Default; options = 1; noFlags = options.AsFlagsToEnum <EnumNoFlags>(); withFlags = options.AsFlagsToEnum <EnumWithFlags>(); assertEquals($"{nameof(EnumNoFlags)} value doesn't match options", EnumNoFlags.Symbol1, noFlags); assertEquals($"{nameof(EnumWithFlags)} value doesn't match options", EnumWithFlags.Symbol1, withFlags); options = 2; noFlags = options.AsFlagsToEnum <EnumNoFlags>(); withFlags = options.AsFlagsToEnum <EnumWithFlags>(); assertEquals($"{nameof(EnumNoFlags)} value doesn't match options", EnumNoFlags.Symbol2, noFlags); assertEquals($"{nameof(EnumWithFlags)} value doesn't match options", EnumWithFlags.Symbol2, withFlags); // Test combined symbols options = 6; AssertThrows <ArgumentOutOfRangeException>(() => noFlags = options.AsFlagsToEnum <EnumNoFlags>(), $"Expected {nameof(ArgumentOutOfRangeException)} not thrown"); withFlags = options.AsFlagsToEnum <EnumWithFlags>(); assertEquals($"{nameof(EnumWithFlags)} value doesn't match options", EnumWithFlags.Symbol2 | EnumWithFlags.Symbol3, withFlags); options = 3; AssertThrows <ArgumentOutOfRangeException>(() => noFlags = options.AsFlagsToEnum <EnumNoFlags>(), $"Expected {nameof(ArgumentOutOfRangeException)} not thrown"); withFlags = options.AsFlagsToEnum <EnumWithFlags>(); assertEquals($"{nameof(EnumWithFlags)} value doesn't match options", EnumWithFlags.Symbol1 | EnumWithFlags.Symbol2, withFlags); // Test default value options = 0; noFlags = options.AsFlagsToEnum <EnumNoFlags>(EnumNoFlags.Symbol3); withFlags = options.AsFlagsToEnum <EnumWithFlags>(EnumWithFlags.Symbol3); assertEquals($"{nameof(EnumNoFlags)} value doesn't match options", EnumNoFlags.Symbol3, noFlags); assertEquals($"{nameof(EnumWithFlags)} value doesn't match options", EnumWithFlags.Symbol3, withFlags); }
public AttributeWithEnumFlagsAttribute(EnumWithFlags value) { }