Skip to content

Commit

Permalink
Use type code for enums
Browse files Browse the repository at this point in the history
  • Loading branch information
Kermalis committed Sep 7, 2020
1 parent 6589690 commit f76dd76
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
18 changes: 9 additions & 9 deletions Source/EndianBinaryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,16 +529,16 @@ public TEnum ReadEnum<TEnum>() where TEnum : struct, Enum
Type enumType = typeof(TEnum);
Type underlyingType = Enum.GetUnderlyingType(enumType);
object value;
switch (underlyingType.FullName)
switch (Type.GetTypeCode(underlyingType))
{
case "System.Byte": value = ReadByte(); break;
case "System.SByte": value = ReadSByte(); break;
case "System.Int16": value = ReadInt16(); break;
case "System.UInt16": value = ReadUInt16(); break;
case "System.Int32": value = ReadInt32(); break;
case "System.UInt32": value = ReadUInt32(); break;
case "System.Int64": value = ReadInt64(); break;
case "System.UInt64": value = ReadUInt64(); break;
case TypeCode.Byte: value = ReadByte(); break;
case TypeCode.SByte: value = ReadSByte(); break;
case TypeCode.Int16: value = ReadInt16(); break;
case TypeCode.UInt16: value = ReadUInt16(); break;
case TypeCode.Int32: value = ReadInt32(); break;
case TypeCode.UInt32: value = ReadUInt32(); break;
case TypeCode.Int64: value = ReadInt64(); break;
case TypeCode.UInt64: value = ReadUInt64(); break;
default: throw new ArgumentOutOfRangeException(nameof(underlyingType));
}
return (TEnum)Enum.ToObject(enumType, value);
Expand Down
20 changes: 10 additions & 10 deletions Source/EndianBinaryWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -702,16 +702,16 @@ public void Write<TEnum>(TEnum value) where TEnum : Enum
// For example, writer.Write((Enum)Enum.Parse(enumType, value))
// No "struct" restriction on writes
Type underlyingType = Enum.GetUnderlyingType(value.GetType());
switch (underlyingType.FullName)
{
case "System.Byte": Write(Convert.ToByte(value)); break;
case "System.SByte": Write(Convert.ToSByte(value)); break;
case "System.Int16": Write(Convert.ToInt16(value)); break;
case "System.UInt16": Write(Convert.ToUInt16(value)); break;
case "System.Int32": Write(Convert.ToInt32(value)); break;
case "System.UInt32": Write(Convert.ToUInt32(value)); break;
case "System.Int64": Write(Convert.ToInt64(value)); break;
case "System.UInt64": Write(Convert.ToUInt64(value)); break;
switch (Type.GetTypeCode(underlyingType))
{
case TypeCode.Byte: Write(Convert.ToByte(value)); break;
case TypeCode.SByte: Write(Convert.ToSByte(value)); break;
case TypeCode.Int16: Write(Convert.ToInt16(value)); break;
case TypeCode.UInt16: Write(Convert.ToUInt16(value)); break;
case TypeCode.Int32: Write(Convert.ToInt32(value)); break;
case TypeCode.UInt32: Write(Convert.ToUInt32(value)); break;
case TypeCode.Int64: Write(Convert.ToInt64(value)); break;
case TypeCode.UInt64: Write(Convert.ToUInt64(value)); break;
default: throw new ArgumentOutOfRangeException(nameof(underlyingType));
}
}
Expand Down

0 comments on commit f76dd76

Please sign in to comment.