I'm having difficulty working with some deprecated enumerations that have several null values. Whenever I call ToString
on one of the non-zero values, everything except the first zero value is turned on.
Is there a way to isolate a nonzero name without resorting to string manipulation or reflection?
//all of the following output "Nada, Zilch, One" Console.WriteLine(TestEnum.One); Console.WriteLine(Convert.ToString(TestEnum.One)); Console.WriteLine(TypeDescriptor.GetConverter(typeof(TestEnum)) .ConvertToString(TestEnum.One)); [Flags] enum TestEnum { Zero = 0, Nada = 0, Zilch = 0, One = 1 }
Edit
I understand that the presence of several elements with the same value is not recommended, however, the specified enumeration is defined in an obsolete assembly, which I cannot change. In fact, there are 12 open enumerations in mscorlib v4 that violate this recommendation, as defined by the following simple LINQ query:
var types = typeof (void).Assembly.GetTypes() .Where(type => type.IsEnum && type.IsPublic && Enum.GetValues(type).Cast<object>() .GroupBy(value => value) .Any(grp => grp.Count() > 1)) .ToList();
Nathan baulch
source share