2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
// See the LICENSE file in the project root for more information.
4
4
5
+ #nullable enable
5
6
using System . Diagnostics ;
6
7
using System . Globalization ;
7
8
using System . Reflection ;
@@ -72,8 +73,7 @@ private string ValueToString()
72
73
case CorElementType . ELEMENT_TYPE_U :
73
74
return Unsafe . As < byte , UIntPtr > ( ref data ) . ToString ( ) ;
74
75
default :
75
- Debug . Fail ( "Invalid primitive type" ) ;
76
- return null ;
76
+ throw new InvalidOperationException ( SR . InvalidOperation_UnknownEnumType ) ;
77
77
}
78
78
}
79
79
@@ -133,12 +133,12 @@ private static string ValueToHexString(object value)
133
133
}
134
134
}
135
135
136
- internal static string GetEnumName ( RuntimeType enumType , ulong ulValue )
136
+ internal static string ? GetEnumName ( RuntimeType enumType , ulong ulValue )
137
137
{
138
138
return GetEnumName ( GetEnumInfo ( enumType ) , ulValue ) ;
139
139
}
140
140
141
- private static string GetEnumName ( EnumInfo enumInfo , ulong ulValue )
141
+ private static string ? GetEnumName ( EnumInfo enumInfo , ulong ulValue )
142
142
{
143
143
int index = Array . BinarySearch ( enumInfo . Values , ulValue ) ;
144
144
if ( index >= 0 )
@@ -149,7 +149,7 @@ private static string GetEnumName(EnumInfo enumInfo, ulong ulValue)
149
149
return null ; // return null so the caller knows to .ToString() the input
150
150
}
151
151
152
- private static string InternalFormat ( RuntimeType enumType , ulong value )
152
+ private static string ? InternalFormat ( RuntimeType enumType , ulong value )
153
153
{
154
154
EnumInfo enumInfo = GetEnumInfo ( enumType ) ;
155
155
@@ -163,12 +163,12 @@ private static string InternalFormat(RuntimeType enumType, ulong value)
163
163
}
164
164
}
165
165
166
- private static string InternalFlagsFormat ( RuntimeType enumType , ulong result )
166
+ private static string ? InternalFlagsFormat ( RuntimeType enumType , ulong result )
167
167
{
168
168
return InternalFlagsFormat ( enumType , GetEnumInfo ( enumType ) , result ) ;
169
169
}
170
170
171
- private static string InternalFlagsFormat ( RuntimeType enumType , EnumInfo enumInfo , ulong resultValue )
171
+ private static string ? InternalFlagsFormat ( RuntimeType enumType , EnumInfo enumInfo , ulong resultValue )
172
172
{
173
173
Debug . Assert ( enumType != null ) ;
174
174
@@ -322,9 +322,9 @@ public static object Parse(Type enumType, string value) =>
322
322
323
323
public static object Parse ( Type enumType , string value , bool ignoreCase )
324
324
{
325
- bool success = TryParse ( enumType , value , ignoreCase , throwOnFailure : true , out object result ) ;
325
+ bool success = TryParse ( enumType , value , ignoreCase , throwOnFailure : true , out object ? result ) ;
326
326
Debug . Assert ( success ) ;
327
- return result ;
327
+ return result ! ;
328
328
}
329
329
330
330
public static TEnum Parse < TEnum > ( string value ) where TEnum : struct =>
@@ -337,13 +337,13 @@ public static TEnum Parse<TEnum>(string value, bool ignoreCase) where TEnum : st
337
337
return result ;
338
338
}
339
339
340
- public static bool TryParse ( Type enumType , string value , out object result ) =>
340
+ public static bool TryParse ( Type enumType , string ? value , out object ? result ) =>
341
341
TryParse ( enumType , value , ignoreCase : false , out result ) ;
342
342
343
- public static bool TryParse ( Type enumType , string value , bool ignoreCase , out object result ) =>
343
+ public static bool TryParse ( Type enumType , string ? value , bool ignoreCase , out object ? result ) =>
344
344
TryParse ( enumType , value , ignoreCase , throwOnFailure : false , out result ) ;
345
345
346
- private static bool TryParse ( Type enumType , string value , bool ignoreCase , bool throwOnFailure , out object result )
346
+ private static bool TryParse ( Type enumType , string ? value , bool ignoreCase , bool throwOnFailure , out object ? result )
347
347
{
348
348
// Validation on the enum type itself. Failures here are considered non-parsing failures
349
349
// and thus always throw rather than returning false.
@@ -413,13 +413,13 @@ private static bool TryParse(Type enumType, string value, bool ignoreCase, bool
413
413
}
414
414
}
415
415
416
- public static bool TryParse < TEnum > ( string value , out TEnum result ) where TEnum : struct =>
416
+ public static bool TryParse < TEnum > ( string ? value , out TEnum result ) where TEnum : struct =>
417
417
TryParse < TEnum > ( value , ignoreCase : false , out result ) ;
418
418
419
- public static bool TryParse < TEnum > ( string value , bool ignoreCase , out TEnum result ) where TEnum : struct =>
419
+ public static bool TryParse < TEnum > ( string ? value , bool ignoreCase , out TEnum result ) where TEnum : struct =>
420
420
TryParse < TEnum > ( value , ignoreCase , throwOnFailure : false , out result ) ;
421
421
422
- private static bool TryParse < TEnum > ( string value , bool ignoreCase , bool throwOnFailure , out TEnum result ) where TEnum : struct
422
+ private static bool TryParse < TEnum > ( string ? value , bool ignoreCase , bool throwOnFailure , out TEnum result ) where TEnum : struct
423
423
{
424
424
// Validation on the enum type itself. Failures here are considered non-parsing failures
425
425
// and thus always throw rather than returning false.
@@ -493,15 +493,15 @@ private static bool TryParse<TEnum>(string value, bool ignoreCase, bool throwOnF
493
493
return parsed ;
494
494
495
495
default :
496
- parsed = TryParseRareEnum ( rt , value , valueSpan , ignoreCase , throwOnFailure , out object objectResult ) ;
496
+ parsed = TryParseRareEnum ( rt , value , valueSpan , ignoreCase , throwOnFailure , out object ? objectResult ) ;
497
497
result = parsed ? ( TEnum ) objectResult : default ;
498
498
return parsed ;
499
499
}
500
500
}
501
501
502
502
/// <summary>Tries to parse the value of an enum with known underlying types that fit in an Int32 (Int32, Int16, and SByte).</summary>
503
503
private static bool TryParseInt32Enum (
504
- RuntimeType enumType , string originalValueString , ReadOnlySpan < char > value , int minInclusive , int maxInclusive , bool ignoreCase , bool throwOnFailure , TypeCode type , out int result )
504
+ RuntimeType enumType , string ? originalValueString , ReadOnlySpan < char > value , int minInclusive , int maxInclusive , bool ignoreCase , bool throwOnFailure , TypeCode type , out int result )
505
505
{
506
506
Debug . Assert (
507
507
enumType . GetEnumUnderlyingType ( ) == typeof ( sbyte ) ||
@@ -542,7 +542,7 @@ private static bool TryParseInt32Enum(
542
542
}
543
543
544
544
/// <summary>Tries to parse the value of an enum with known underlying types that fit in a UInt32 (UInt32, UInt16, and Byte).</summary>
545
- private static bool TryParseUInt32Enum ( RuntimeType enumType , string originalValueString , ReadOnlySpan < char > value , uint maxInclusive , bool ignoreCase , bool throwOnFailure , TypeCode type , out uint result )
545
+ private static bool TryParseUInt32Enum ( RuntimeType enumType , string ? originalValueString , ReadOnlySpan < char > value , uint maxInclusive , bool ignoreCase , bool throwOnFailure , TypeCode type , out uint result )
546
546
{
547
547
Debug . Assert (
548
548
enumType . GetEnumUnderlyingType ( ) == typeof ( byte ) ||
@@ -583,7 +583,7 @@ private static bool TryParseUInt32Enum(RuntimeType enumType, string originalValu
583
583
}
584
584
585
585
/// <summary>Tries to parse the value of an enum with Int64 as the underlying type.</summary>
586
- private static bool TryParseInt64Enum ( RuntimeType enumType , string originalValueString , ReadOnlySpan < char > value , bool ignoreCase , bool throwOnFailure , out long result )
586
+ private static bool TryParseInt64Enum ( RuntimeType enumType , string ? originalValueString , ReadOnlySpan < char > value , bool ignoreCase , bool throwOnFailure , out long result )
587
587
{
588
588
Debug . Assert ( enumType . GetEnumUnderlyingType ( ) == typeof ( long ) ) ;
589
589
@@ -615,7 +615,7 @@ private static bool TryParseInt64Enum(RuntimeType enumType, string originalValue
615
615
}
616
616
617
617
/// <summary>Tries to parse the value of an enum with UInt64 as the underlying type.</summary>
618
- private static bool TryParseUInt64Enum ( RuntimeType enumType , string originalValueString , ReadOnlySpan < char > value , bool ignoreCase , bool throwOnFailure , out ulong result )
618
+ private static bool TryParseUInt64Enum ( RuntimeType enumType , string ? originalValueString , ReadOnlySpan < char > value , bool ignoreCase , bool throwOnFailure , out ulong result )
619
619
{
620
620
Debug . Assert ( enumType . GetEnumUnderlyingType ( ) == typeof ( ulong ) ) ;
621
621
@@ -646,7 +646,7 @@ private static bool TryParseUInt64Enum(RuntimeType enumType, string originalValu
646
646
}
647
647
648
648
/// <summary>Tries to parse the value of an enum with an underlying type that can't be expressed in C# (e.g. char, bool, double, etc.)</summary>
649
- private static bool TryParseRareEnum ( RuntimeType enumType , string originalValueString , ReadOnlySpan < char > value , bool ignoreCase , bool throwOnFailure , out object result )
649
+ private static bool TryParseRareEnum ( RuntimeType enumType , string ? originalValueString , ReadOnlySpan < char > value , bool ignoreCase , bool throwOnFailure , out object ? result )
650
650
{
651
651
Debug . Assert (
652
652
enumType . GetEnumUnderlyingType ( ) != typeof ( sbyte ) &&
@@ -693,7 +693,7 @@ private static bool TryParseRareEnum(RuntimeType enumType, string originalValueS
693
693
return false ;
694
694
}
695
695
696
- private static bool TryParseByName ( RuntimeType enumType , string originalValueString , ReadOnlySpan < char > value , bool ignoreCase , bool throwOnFailure , out ulong result )
696
+ private static bool TryParseByName ( RuntimeType enumType , string ? originalValueString , ReadOnlySpan < char > value , bool ignoreCase , bool throwOnFailure , out ulong result )
697
697
{
698
698
// Find the field. Let's assume that these are always static classes because the class is an enum.
699
699
EnumInfo enumInfo = GetEnumInfo ( enumType ) ;
@@ -862,19 +862,19 @@ public static string Format(Type enumType, object value, string format)
862
862
{
863
863
case 'G' :
864
864
case 'g' :
865
- return GetEnumName ( rtType , ToUInt64 ( value ) ) ?? value . ToString ( ) ;
865
+ return GetEnumName ( rtType , ToUInt64 ( value ) ) ?? value . ToString ( ) ! ;
866
866
867
867
case 'D' :
868
868
case 'd' :
869
- return value . ToString ( ) ;
869
+ return value . ToString ( ) ! ;
870
870
871
871
case 'X' :
872
872
case 'x' :
873
873
return ValueToHexString ( value ) ;
874
874
875
875
case 'F' :
876
876
case 'f' :
877
- return InternalFlagsFormat ( rtType , ToUInt64 ( value ) ) ?? value . ToString ( ) ;
877
+ return InternalFlagsFormat ( rtType , ToUInt64 ( value ) ) ?? value . ToString ( ) ! ;
878
878
}
879
879
}
880
880
@@ -917,8 +917,7 @@ internal object GetValue()
917
917
case CorElementType . ELEMENT_TYPE_U :
918
918
return Unsafe . As < byte , UIntPtr > ( ref data ) ;
919
919
default :
920
- Debug . Fail ( "Invalid primitive type" ) ;
921
- return null ;
920
+ throw new InvalidOperationException ( SR . InvalidOperation_UnknownEnumType ) ;
922
921
}
923
922
}
924
923
@@ -953,8 +952,7 @@ private ulong ToUInt64()
953
952
case CorElementType . ELEMENT_TYPE_U :
954
953
return ( ulong ) Unsafe . As < byte , UIntPtr > ( ref data ) ;
955
954
default :
956
- Debug . Fail ( "Invalid primitive type" ) ;
957
- return 0 ;
955
+ throw new InvalidOperationException ( SR . InvalidOperation_UnknownEnumType ) ;
958
956
}
959
957
}
960
958
@@ -999,8 +997,7 @@ public override int GetHashCode()
999
997
case CorElementType . ELEMENT_TYPE_U :
1000
998
return Unsafe . As < byte , UIntPtr > ( ref data ) . GetHashCode ( ) ;
1001
999
default :
1002
- Debug . Fail ( "Invalid primitive type" ) ;
1003
- return 0 ;
1000
+ throw new InvalidOperationException ( SR . InvalidOperation_UnknownEnumType ) ;
1004
1001
}
1005
1002
}
1006
1003
@@ -1019,14 +1016,14 @@ public override string ToString()
1019
1016
1020
1017
#region IFormattable
1021
1018
[ Obsolete ( "The provider argument is not used. Please use ToString(String)." ) ]
1022
- public string ToString ( string format , IFormatProvider provider )
1019
+ public string ToString ( string ? format , IFormatProvider ? provider )
1023
1020
{
1024
1021
return ToString ( format ) ;
1025
1022
}
1026
1023
#endregion
1027
1024
1028
1025
#region Public Methods
1029
- public string ToString ( string format )
1026
+ public string ToString ( string ? format )
1030
1027
{
1031
1028
if ( string . IsNullOrEmpty ( format ) )
1032
1029
{
@@ -1059,7 +1056,7 @@ public string ToString(string format)
1059
1056
}
1060
1057
1061
1058
[ Obsolete ( "The provider argument is not used. Please use ToString()." ) ]
1062
- public string ToString ( IFormatProvider provider )
1059
+ public string ToString ( IFormatProvider ? provider )
1063
1060
{
1064
1061
return ToString ( ) ;
1065
1062
}
@@ -1096,77 +1093,77 @@ public TypeCode GetTypeCode()
1096
1093
}
1097
1094
}
1098
1095
1099
- bool IConvertible . ToBoolean ( IFormatProvider provider )
1096
+ bool IConvertible . ToBoolean ( IFormatProvider ? provider )
1100
1097
{
1101
1098
return Convert . ToBoolean ( GetValue ( ) , CultureInfo . CurrentCulture ) ;
1102
1099
}
1103
1100
1104
- char IConvertible . ToChar ( IFormatProvider provider )
1101
+ char IConvertible . ToChar ( IFormatProvider ? provider )
1105
1102
{
1106
1103
return Convert . ToChar ( GetValue ( ) , CultureInfo . CurrentCulture ) ;
1107
1104
}
1108
1105
1109
- sbyte IConvertible . ToSByte ( IFormatProvider provider )
1106
+ sbyte IConvertible . ToSByte ( IFormatProvider ? provider )
1110
1107
{
1111
1108
return Convert . ToSByte ( GetValue ( ) , CultureInfo . CurrentCulture ) ;
1112
1109
}
1113
1110
1114
- byte IConvertible . ToByte ( IFormatProvider provider )
1111
+ byte IConvertible . ToByte ( IFormatProvider ? provider )
1115
1112
{
1116
1113
return Convert . ToByte ( GetValue ( ) , CultureInfo . CurrentCulture ) ;
1117
1114
}
1118
1115
1119
- short IConvertible . ToInt16 ( IFormatProvider provider )
1116
+ short IConvertible . ToInt16 ( IFormatProvider ? provider )
1120
1117
{
1121
1118
return Convert . ToInt16 ( GetValue ( ) , CultureInfo . CurrentCulture ) ;
1122
1119
}
1123
1120
1124
- ushort IConvertible . ToUInt16 ( IFormatProvider provider )
1121
+ ushort IConvertible . ToUInt16 ( IFormatProvider ? provider )
1125
1122
{
1126
1123
return Convert . ToUInt16 ( GetValue ( ) , CultureInfo . CurrentCulture ) ;
1127
1124
}
1128
1125
1129
- int IConvertible . ToInt32 ( IFormatProvider provider )
1126
+ int IConvertible . ToInt32 ( IFormatProvider ? provider )
1130
1127
{
1131
1128
return Convert . ToInt32 ( GetValue ( ) , CultureInfo . CurrentCulture ) ;
1132
1129
}
1133
1130
1134
- uint IConvertible . ToUInt32 ( IFormatProvider provider )
1131
+ uint IConvertible . ToUInt32 ( IFormatProvider ? provider )
1135
1132
{
1136
1133
return Convert . ToUInt32 ( GetValue ( ) , CultureInfo . CurrentCulture ) ;
1137
1134
}
1138
1135
1139
- long IConvertible . ToInt64 ( IFormatProvider provider )
1136
+ long IConvertible . ToInt64 ( IFormatProvider ? provider )
1140
1137
{
1141
1138
return Convert . ToInt64 ( GetValue ( ) , CultureInfo . CurrentCulture ) ;
1142
1139
}
1143
1140
1144
- ulong IConvertible . ToUInt64 ( IFormatProvider provider )
1141
+ ulong IConvertible . ToUInt64 ( IFormatProvider ? provider )
1145
1142
{
1146
1143
return Convert . ToUInt64 ( GetValue ( ) , CultureInfo . CurrentCulture ) ;
1147
1144
}
1148
1145
1149
- float IConvertible . ToSingle ( IFormatProvider provider )
1146
+ float IConvertible . ToSingle ( IFormatProvider ? provider )
1150
1147
{
1151
1148
return Convert . ToSingle ( GetValue ( ) , CultureInfo . CurrentCulture ) ;
1152
1149
}
1153
1150
1154
- double IConvertible . ToDouble ( IFormatProvider provider )
1151
+ double IConvertible . ToDouble ( IFormatProvider ? provider )
1155
1152
{
1156
1153
return Convert . ToDouble ( GetValue ( ) , CultureInfo . CurrentCulture ) ;
1157
1154
}
1158
1155
1159
- decimal IConvertible . ToDecimal ( IFormatProvider provider )
1156
+ decimal IConvertible . ToDecimal ( IFormatProvider ? provider )
1160
1157
{
1161
1158
return Convert . ToDecimal ( GetValue ( ) , CultureInfo . CurrentCulture ) ;
1162
1159
}
1163
1160
1164
- DateTime IConvertible . ToDateTime ( IFormatProvider provider )
1161
+ DateTime IConvertible . ToDateTime ( IFormatProvider ? provider )
1165
1162
{
1166
1163
throw new InvalidCastException ( SR . Format ( SR . InvalidCast_FromTo , "Enum" , "DateTime" ) ) ;
1167
1164
}
1168
1165
1169
- object IConvertible . ToType ( Type type , IFormatProvider provider )
1166
+ object IConvertible . ToType ( Type type , IFormatProvider ? provider )
1170
1167
{
1171
1168
return Convert . DefaultToType ( ( IConvertible ) this , type , provider ) ;
1172
1169
}
0 commit comments