diff options
author | csharptest <roger@csharptest.net> | 2012-10-14 20:14:58 -0500 |
---|---|---|
committer | rogerk <devnull@localhost> | 2012-10-14 20:14:58 -0500 |
commit | 9f787cc64e47333ea717681c874843b44fee60e7 (patch) | |
tree | 6ba68fbc5021966e8945c1637e90e2b1f5a3b73d /src | |
parent | efc90f40009924c2e2863cfadbf4f937dd045e9d (diff) | |
download | protobuf-9f787cc64e47333ea717681c874843b44fee60e7.tar.gz protobuf-9f787cc64e47333ea717681c874843b44fee60e7.tar.bz2 protobuf-9f787cc64e47333ea717681c874843b44fee60e7.zip |
Removed uses of reflection for enumeration of enum members
Diffstat (limited to 'src')
-rw-r--r-- | src/ProtocolBuffers/EnumLite.cs | 37 |
1 files changed, 7 insertions, 30 deletions
diff --git a/src/ProtocolBuffers/EnumLite.cs b/src/ProtocolBuffers/EnumLite.cs index 450ecb77..978f4e51 100644 --- a/src/ProtocolBuffers/EnumLite.cs +++ b/src/ProtocolBuffers/EnumLite.cs @@ -93,51 +93,28 @@ namespace Google.ProtocolBuffers }
}
- private readonly SortedList<int, IEnumLite> items;
-
- public EnumLiteMap()
+ public IEnumLite FindValueByNumber(int number)
{
- 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)))
+ 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 |