Skip to content

Commit

Permalink
Remove Convert.ToByte usage from Enum (#45004)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Nov 20, 2020
1 parent c409a32 commit f2e83d7
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/libraries/System.Private.CoreLib/src/System/Enum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private string ValueToHexString()
case CorElementType.ELEMENT_TYPE_U1:
return data.ToString("X2", null);
case CorElementType.ELEMENT_TYPE_BOOLEAN:
return Convert.ToByte(Unsafe.As<byte, bool>(ref data)).ToString("X2", null);
return data != 0 ? "01" : "00";
case CorElementType.ELEMENT_TYPE_I2:
case CorElementType.ELEMENT_TYPE_U2:
case CorElementType.ELEMENT_TYPE_CHAR:
Expand All @@ -91,7 +91,7 @@ private static string ValueToHexString(object value)
{
TypeCode.SByte => ((byte)(sbyte)value).ToString("X2", null),
TypeCode.Byte => ((byte)value).ToString("X2", null),
TypeCode.Boolean => Convert.ToByte((bool)value).ToString("X2", null), // direct cast from bool to byte is not allowed
TypeCode.Boolean => ((bool)value) ? "01" : "00",
TypeCode.Int16 => ((ushort)(short)value).ToString("X4", null),
TypeCode.UInt16 => ((ushort)value).ToString("X4", null),
TypeCode.Char => ((ushort)(char)value).ToString("X4", null),
Expand Down Expand Up @@ -245,10 +245,10 @@ internal static ulong ToUInt64(object value)
{
TypeCode.SByte => (ulong)(sbyte)value,
TypeCode.Byte => (byte)value,
TypeCode.Boolean => Convert.ToByte((bool)value), // direct cast from bool to byte is not allowed
TypeCode.Boolean => (bool)value ? 1UL : 0UL,
TypeCode.Int16 => (ulong)(short)value,
TypeCode.UInt16 => (ushort)value,
TypeCode.Char => (ushort)(char)value,
TypeCode.Char => (char)value,
TypeCode.UInt32 => (uint)value,
TypeCode.Int32 => (ulong)(int)value,
TypeCode.UInt64 => (ulong)value,
Expand All @@ -263,7 +263,7 @@ private static ulong ToUInt64<TEnum>(TEnum value) where TEnum : struct, Enum =>
{
TypeCode.SByte => (ulong)Unsafe.As<TEnum, sbyte>(ref value),
TypeCode.Byte => Unsafe.As<TEnum, byte>(ref value),
TypeCode.Boolean => Convert.ToByte(Unsafe.As<TEnum, bool>(ref value)),
TypeCode.Boolean => Unsafe.As<TEnum, bool>(ref value) ? 1UL : 0UL,
TypeCode.Int16 => (ulong)Unsafe.As<TEnum, short>(ref value),
TypeCode.UInt16 => Unsafe.As<TEnum, ushort>(ref value),
TypeCode.Char => Unsafe.As<TEnum, char>(ref value),
Expand Down Expand Up @@ -1197,7 +1197,7 @@ DateTime IConvertible.ToDateTime(IFormatProvider? provider)

object IConvertible.ToType(Type type, IFormatProvider? provider)
{
return Convert.DefaultToType((IConvertible)this, type, provider);
return Convert.DefaultToType(this, type, provider);
}
#endregion

Expand Down Expand Up @@ -1234,7 +1234,7 @@ private static object ToObject(Type enumType, char value) =>
InternalBoxEnum(ValidateRuntimeType(enumType), value);

private static object ToObject(Type enumType, bool value) =>
InternalBoxEnum(ValidateRuntimeType(enumType), value ? 1 : 0);
InternalBoxEnum(ValidateRuntimeType(enumType), value ? 1L : 0L);

#endregion

Expand Down

0 comments on commit f2e83d7

Please sign in to comment.