From 9f787cc64e47333ea717681c874843b44fee60e7 Mon Sep 17 00:00:00 2001 From: csharptest Date: Sun, 14 Oct 2012 20:14:58 -0500 Subject: [PATCH] Removed uses of reflection for enumeration of enum members --- src/ProtocolBuffers/EnumLite.cs | 37 +++++++-------------------------- 1 file changed, 7 insertions(+), 30 deletions(-) diff --git a/src/ProtocolBuffers/EnumLite.cs b/src/ProtocolBuffers/EnumLite.cs index 450ecb7760..978f4e51ab 100644 --- a/src/ProtocolBuffers/EnumLite.cs +++ b/src/ProtocolBuffers/EnumLite.cs @@ -93,51 +93,28 @@ namespace Google.ProtocolBuffers } } - private readonly SortedList items; - - public EnumLiteMap() + public IEnumLite FindValueByNumber(int number) { - items = new SortedList(); -#if SILVERLIGHT || COMPACT_FRAMEWORK - // Silverlight doesn't support Enum.GetValues - // TODO(jonskeet): Validate that this reflection is permitted, e.g. in Windows Phone 7 - foreach (System.Reflection.FieldInfo fi in typeof(TEnum).GetFields(System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public)) - { - TEnum evalue = (TEnum) fi.GetValue(null); - items.Add(Convert.ToInt32(evalue), new EnumValue(evalue)); - } -#else - foreach (TEnum evalue in Enum.GetValues(typeof(TEnum))) + if (Enum.IsDefined(typeof(TEnum), number)) { - items.Add(Convert.ToInt32(evalue), new EnumValue(evalue)); + return new EnumValue((TEnum)(object)number); } -#endif - } - - IEnumLite IEnumLiteMap.FindValueByNumber(int number) - { - return FindValueByNumber(number); - } - - public IEnumLite FindValueByNumber(int number) - { - IEnumLite val; - return items.TryGetValue(number, out val) ? val : null; + return null; } public IEnumLite FindValueByName(string name) { - IEnumLite val; if (Enum.IsDefined(typeof(TEnum), name)) { - return items.TryGetValue((int) Enum.Parse(typeof(TEnum), name, false), out val) ? val : null; + object evalue = Enum.Parse(typeof(TEnum), name, false); + return new EnumValue((TEnum)evalue); } return null; } public bool IsValidValue(IEnumLite value) { - return items.ContainsKey(value.Number); + return Enum.IsDefined(typeof(TEnum), value.Number); } } } \ No newline at end of file