Removed uses of reflection for enumeration of enum members

pull/288/head
csharptest 12 years ago committed by rogerk
parent efc90f4000
commit 9f787cc64e
  1. 37
      src/ProtocolBuffers/EnumLite.cs

@ -93,51 +93,28 @@ namespace Google.ProtocolBuffers
} }
} }
private readonly SortedList<int, IEnumLite> items; public IEnumLite FindValueByNumber(int number)
public EnumLiteMap()
{
items = new SortedList<int, IEnumLite>();
#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)))
{ {
items.Add(Convert.ToInt32(evalue), new EnumValue(evalue)); if (Enum.IsDefined(typeof(TEnum), number))
}
#endif
}
IEnumLite IEnumLiteMap.FindValueByNumber(int number)
{ {
return FindValueByNumber(number); return new EnumValue((TEnum)(object)number);
} }
return null;
public IEnumLite FindValueByNumber(int number)
{
IEnumLite val;
return items.TryGetValue(number, out val) ? val : null;
} }
public IEnumLite FindValueByName(string name) public IEnumLite FindValueByName(string name)
{ {
IEnumLite val;
if (Enum.IsDefined(typeof(TEnum), name)) 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; return null;
} }
public bool IsValidValue(IEnumLite value) public bool IsValidValue(IEnumLite value)
{ {
return items.ContainsKey(value.Number); return Enum.IsDefined(typeof(TEnum), value.Number);
} }
} }
} }
Loading…
Cancel
Save