diff options
Diffstat (limited to 'src/mscorlib/src/System/Enum.cs')
-rw-r--r-- | src/mscorlib/src/System/Enum.cs | 208 |
1 files changed, 99 insertions, 109 deletions
diff --git a/src/mscorlib/src/System/Enum.cs b/src/mscorlib/src/System/Enum.cs index 97cfdf1b23..489f36739c 100644 --- a/src/mscorlib/src/System/Enum.cs +++ b/src/mscorlib/src/System/Enum.cs @@ -12,7 +12,7 @@ using System.Runtime.Versioning; using System.Diagnostics; using System.Diagnostics.Contracts; -namespace System +namespace System { [Serializable] public abstract class Enum : ValueType, IComparable, IFormattable, IConvertible @@ -69,7 +69,7 @@ namespace System case CorElementType.U8: return (*(ulong*)pValue).ToString("X16", null); default: - throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_UnknownEnumType")); + throw new InvalidOperationException(SR.InvalidOperation_UnknownEnumType); } } } @@ -103,7 +103,7 @@ namespace System return ((UInt64)(Int64)value).ToString("X16", null); // All unsigned types will be directly cast default: - throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_UnknownEnumType")); + throw new InvalidOperationException(SR.InvalidOperation_UnknownEnumType); } } @@ -139,7 +139,7 @@ namespace System } } - private static String InternalFlagsFormat(RuntimeType eT,ulong result) + private static String InternalFlagsFormat(RuntimeType eT, ulong result) { // These values are sorted by value. Don't change this TypeValuesAndNames entry = GetCachedValuesAndNames(eT, true); @@ -250,7 +250,7 @@ namespace System break; // All unsigned types will be directly cast default: - throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_UnknownEnumType")); + throw new InvalidOperationException(SR.InvalidOperation_UnknownEnumType); } return result; @@ -273,11 +273,11 @@ namespace System #region Public Static Methods private enum ParseFailureKind { - None = 0, - Argument = 1, - ArgumentNull = 2, + None = 0, + Argument = 1, + ArgumentNull = 2, ArgumentWithParameter = 3, - UnhandledException = 4 + UnhandledException = 4 } // This will store the result of the parsing. @@ -316,20 +316,20 @@ namespace System switch (m_failure) { case ParseFailureKind.Argument: - return new ArgumentException(Environment.GetResourceString(m_failureMessageID)); + return new ArgumentException(SR.GetResourceString(m_failureMessageID)); case ParseFailureKind.ArgumentNull: return new ArgumentNullException(m_failureParameter); case ParseFailureKind.ArgumentWithParameter: - return new ArgumentException(Environment.GetResourceString(m_failureMessageID, m_failureMessageFormatArgument)); + return new ArgumentException(SR.Format(SR.GetResourceString(m_failureMessageID), m_failureMessageFormatArgument)); case ParseFailureKind.UnhandledException: return m_innerException; default: Debug.Assert(false, "Unknown EnumParseFailure: " + m_failure); - return new ArgumentException(Environment.GetResourceString("Arg_EnumValueNotFound")); + return new ArgumentException(SR.Arg_EnumValueNotFound); } } } @@ -362,7 +362,7 @@ namespace System bool retValue; if (retValue = TryParseEnum(typeof(TEnum), value, ignoreCase, ref parseResult)) - result = (TEnum)parseResult.parsedEnum; + result = (TEnum)parseResult.parsedEnum; return retValue; } @@ -402,12 +402,13 @@ namespace System RuntimeType rtType = enumType as RuntimeType; if (rtType == null) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeType, nameof(enumType)); if (!enumType.IsEnum) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeEnum, nameof(enumType)); - if (value == null) { + if (value == null) + { parseResult.SetFailure(ParseFailureKind.ArgumentNull, nameof(value)); return false; } @@ -421,7 +422,8 @@ namespace System break; } } - if (firstNonWhitespaceIndex == -1) { + if (firstNonWhitespaceIndex == -1) + { parseResult.SetFailure(ParseFailureKind.Argument, "Arg_MustContainEnumInfo", null); return false; } @@ -466,8 +468,8 @@ namespace System String[] enumNames = entry.Names; ulong[] enumValues = entry.Values; - StringComparison comparison = ignoreCase ? - StringComparison.OrdinalIgnoreCase : + StringComparison comparison = ignoreCase ? + StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal; int valueIndex = firstNonWhitespaceIndex; @@ -490,7 +492,7 @@ namespace System bool success = false; for (int i = 0; i < enumNames.Length; i++) { - if (enumNames[i].Length == valueSubstringLength && + if (enumNames[i].Length == valueSubstringLength && string.Compare(enumNames[i], 0, value, valueIndex, valueSubstringLength, comparison) == 0) { result |= enumValues[i]; @@ -553,7 +555,7 @@ namespace System // Get all of the values return GetCachedValuesAndNames(enumType, false).Values; } - + public static String GetName(Type enumType, Object value) { if (enumType == null) @@ -590,28 +592,28 @@ namespace System switch (typeCode) { - case TypeCode.Int32 : + case TypeCode.Int32: return ToObject(enumType, (int)value); - case TypeCode.SByte : + case TypeCode.SByte: return ToObject(enumType, (sbyte)value); - case TypeCode.Int16 : + case TypeCode.Int16: return ToObject(enumType, (short)value); - case TypeCode.Int64 : + case TypeCode.Int64: return ToObject(enumType, (long)value); - case TypeCode.UInt32 : + case TypeCode.UInt32: return ToObject(enumType, (uint)value); - case TypeCode.Byte : + case TypeCode.Byte: return ToObject(enumType, (byte)value); - case TypeCode.UInt16 : + case TypeCode.UInt16: return ToObject(enumType, (ushort)value); - case TypeCode.UInt64 : + case TypeCode.UInt64: return ToObject(enumType, (ulong)value); case TypeCode.Char: @@ -622,7 +624,7 @@ namespace System default: // All unsigned types will be directly cast - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnumBaseTypeOrEnum"), nameof(value)); + throw new ArgumentException(SR.Arg_MustBeEnumBaseTypeOrEnum, nameof(value)); } } @@ -635,14 +637,14 @@ namespace System return enumType.IsEnumDefined(value); } - + public static String Format(Type enumType, Object value, String format) { if (enumType == null) throw new ArgumentNullException(nameof(enumType)); if (!enumType.IsEnum) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeEnum, nameof(enumType)); if (value == null) throw new ArgumentNullException(nameof(value)); @@ -653,7 +655,7 @@ namespace System RuntimeType rtType = enumType as RuntimeType; if (rtType == null) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeType, nameof(enumType)); // Check if both of them are of the same type Type valueType = value.GetType(); @@ -661,31 +663,32 @@ namespace System Type underlyingType = GetUnderlyingType(enumType); // If the value is an Enum then we need to extract the underlying value from it - if (valueType.IsEnum) { - + if (valueType.IsEnum) + { if (!valueType.IsEquivalentTo(enumType)) - throw new ArgumentException(Environment.GetResourceString("Arg_EnumAndObjectMustBeSameType", valueType.ToString(), enumType.ToString())); + throw new ArgumentException(SR.Format(SR.Arg_EnumAndObjectMustBeSameType, valueType.ToString(), enumType.ToString())); if (format.Length != 1) { // all acceptable format string are of length 1 - throw new FormatException(Environment.GetResourceString("Format_InvalidEnumFormatSpecification")); + throw new FormatException(SR.Format_InvalidEnumFormatSpecification); } return ((Enum)value).ToString(format); } // The value must be of the same type as the Underlying type of the Enum - else if (valueType != underlyingType) { - throw new ArgumentException(Environment.GetResourceString("Arg_EnumFormatUnderlyingTypeAndObjectMustBeSameType", valueType.ToString(), underlyingType.ToString())); + else if (valueType != underlyingType) + { + throw new ArgumentException(SR.Format(SR.Arg_EnumFormatUnderlyingTypeAndObjectMustBeSameType, valueType.ToString(), underlyingType.ToString())); } if (format.Length != 1) { // all acceptable format string are of length 1 - throw new FormatException(Environment.GetResourceString("Format_InvalidEnumFormatSpecification")); + throw new FormatException(SR.Format_InvalidEnumFormatSpecification); } char formatCh = format[0]; if (formatCh == 'G' || formatCh == 'g') - return GetEnumName(rtType, ToUInt64(value)); + return GetEnumName(rtType, ToUInt64(value)) ?? value.ToString(); if (formatCh == 'D' || formatCh == 'd') return value.ToString(); @@ -696,7 +699,7 @@ namespace System if (formatCh == 'F' || formatCh == 'f') return Enum.InternalFlagsFormat(rtType, ToUInt64(value)) ?? value.ToString(); - throw new FormatException(Environment.GetResourceString("Format_InvalidEnumFormatSpecification")); + throw new FormatException(SR.Format_InvalidEnumFormatSpecification); } #endregion @@ -884,7 +887,7 @@ namespace System #endregion #region IFormattable - [Obsolete("The provider argument is not used. Please use ToString(String).")] + [Obsolete("The provider argument is not used. Please use ToString(String).")] public String ToString(String format, IFormatProvider provider) { return ToString(format); @@ -900,10 +903,10 @@ namespace System if (this == null) throw new NullReferenceException(); Contract.EndContractBlock(); - + int ret = InternalCompareTo(this, target); - if (ret < retIncompatibleMethodTables) + if (ret < retIncompatibleMethodTables) { // -1, 0 and 1 are the normal return codes return ret; @@ -913,26 +916,26 @@ namespace System Type thisType = this.GetType(); Type targetType = target.GetType(); - throw new ArgumentException(Environment.GetResourceString("Arg_EnumAndObjectMustBeSameType", - targetType.ToString(), thisType.ToString())); + throw new ArgumentException(SR.Format(SR.Arg_EnumAndObjectMustBeSameType, targetType.ToString(), thisType.ToString())); } else { // assert valid return code (3) Debug.Assert(ret == retInvalidEnumType, "Enum.InternalCompareTo return code was invalid"); - - throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_UnknownEnumType")); + + throw new InvalidOperationException(SR.InvalidOperation_UnknownEnumType); } } #endregion #region Public Methods - public String ToString(String format) { + public String ToString(String format) + { char formatCh; if (format == null || format.Length == 0) formatCh = 'G'; else if (format.Length != 1) - throw new FormatException(Environment.GetResourceString("Format_InvalidEnumFormatSpecification")); + throw new FormatException(SR.Format_InvalidEnumFormatSpecification); else formatCh = format[0]; @@ -948,7 +951,7 @@ namespace System if (formatCh == 'F' || formatCh == 'f') return InternalFlagsFormat((RuntimeType)GetType(), ToUInt64()) ?? GetValue().ToString(); - throw new FormatException(Environment.GetResourceString("Format_InvalidEnumFormatSpecification")); + throw new FormatException(SR.Format_InvalidEnumFormatSpecification); } [Obsolete("The provider argument is not used. Please use ToString().")] @@ -957,13 +960,15 @@ namespace System return ToString(); } - public Boolean HasFlag(Enum flag) { + public Boolean HasFlag(Enum flag) + { if (flag == null) throw new ArgumentNullException(nameof(flag)); Contract.EndContractBlock(); - - if (!this.GetType().IsEquivalentTo(flag.GetType())) { - throw new ArgumentException(Environment.GetResourceString("Argument_EnumTypeDoesNotMatch", flag.GetType(), this.GetType())); + + if (!this.GetType().IsEquivalentTo(flag.GetType())) + { + throw new ArgumentException(SR.Format(SR.Argument_EnumTypeDoesNotMatch, flag.GetType(), this.GetType())); } return InternalHasFlag(flag); @@ -997,101 +1002,86 @@ namespace System case CorElementType.U8: return TypeCode.UInt64; default: - throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_UnknownEnumType")); + throw new InvalidOperationException(SR.InvalidOperation_UnknownEnumType); } } - /// <internalonly/> - bool IConvertible.ToBoolean(IFormatProvider provider) + bool IConvertible.ToBoolean(IFormatProvider provider) { return Convert.ToBoolean(GetValue(), CultureInfo.CurrentCulture); } - /// <internalonly/> - char IConvertible.ToChar(IFormatProvider provider) + char IConvertible.ToChar(IFormatProvider provider) { return Convert.ToChar(GetValue(), CultureInfo.CurrentCulture); } - /// <internalonly/> - sbyte IConvertible.ToSByte(IFormatProvider provider) + sbyte IConvertible.ToSByte(IFormatProvider provider) { return Convert.ToSByte(GetValue(), CultureInfo.CurrentCulture); } - /// <internalonly/> - byte IConvertible.ToByte(IFormatProvider provider) + byte IConvertible.ToByte(IFormatProvider provider) { return Convert.ToByte(GetValue(), CultureInfo.CurrentCulture); } - /// <internalonly/> - short IConvertible.ToInt16(IFormatProvider provider) + short IConvertible.ToInt16(IFormatProvider provider) { return Convert.ToInt16(GetValue(), CultureInfo.CurrentCulture); } - /// <internalonly/> - ushort IConvertible.ToUInt16(IFormatProvider provider) + ushort IConvertible.ToUInt16(IFormatProvider provider) { return Convert.ToUInt16(GetValue(), CultureInfo.CurrentCulture); } - /// <internalonly/> - int IConvertible.ToInt32(IFormatProvider provider) + int IConvertible.ToInt32(IFormatProvider provider) { return Convert.ToInt32(GetValue(), CultureInfo.CurrentCulture); } - /// <internalonly/> - uint IConvertible.ToUInt32(IFormatProvider provider) + uint IConvertible.ToUInt32(IFormatProvider provider) { return Convert.ToUInt32(GetValue(), CultureInfo.CurrentCulture); } - /// <internalonly/> - long IConvertible.ToInt64(IFormatProvider provider) + long IConvertible.ToInt64(IFormatProvider provider) { return Convert.ToInt64(GetValue(), CultureInfo.CurrentCulture); } - /// <internalonly/> - ulong IConvertible.ToUInt64(IFormatProvider provider) + ulong IConvertible.ToUInt64(IFormatProvider provider) { return Convert.ToUInt64(GetValue(), CultureInfo.CurrentCulture); } - /// <internalonly/> - float IConvertible.ToSingle(IFormatProvider provider) + float IConvertible.ToSingle(IFormatProvider provider) { return Convert.ToSingle(GetValue(), CultureInfo.CurrentCulture); } - /// <internalonly/> - double IConvertible.ToDouble(IFormatProvider provider) + double IConvertible.ToDouble(IFormatProvider provider) { return Convert.ToDouble(GetValue(), CultureInfo.CurrentCulture); } - /// <internalonly/> - Decimal IConvertible.ToDecimal(IFormatProvider provider) + Decimal IConvertible.ToDecimal(IFormatProvider provider) { return Convert.ToDecimal(GetValue(), CultureInfo.CurrentCulture); } - /// <internalonly/> - DateTime IConvertible.ToDateTime(IFormatProvider provider) + DateTime IConvertible.ToDateTime(IFormatProvider provider) { - throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", "Enum", "DateTime")); + throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "Enum", "DateTime")); } - /// <internalonly/> - Object IConvertible.ToType(Type type, IFormatProvider provider) + Object IConvertible.ToType(Type type, IFormatProvider provider) { return Convert.DefaultToType((IConvertible)this, type, provider); } #endregion - + #region ToObject [CLSCompliant(false)] public static Object ToObject(Type enumType, sbyte value) @@ -1099,11 +1089,11 @@ namespace System if (enumType == null) throw new ArgumentNullException(nameof(enumType)); if (!enumType.IsEnum) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeEnum, nameof(enumType)); Contract.EndContractBlock(); RuntimeType rtType = enumType as RuntimeType; if (rtType == null) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeType, nameof(enumType)); return InternalBoxEnum(rtType, value); } @@ -1112,11 +1102,11 @@ namespace System if (enumType == null) throw new ArgumentNullException(nameof(enumType)); if (!enumType.IsEnum) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeEnum, nameof(enumType)); Contract.EndContractBlock(); RuntimeType rtType = enumType as RuntimeType; if (rtType == null) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeType, nameof(enumType)); return InternalBoxEnum(rtType, value); } @@ -1125,11 +1115,11 @@ namespace System if (enumType == null) throw new ArgumentNullException(nameof(enumType)); if (!enumType.IsEnum) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeEnum, nameof(enumType)); Contract.EndContractBlock(); RuntimeType rtType = enumType as RuntimeType; if (rtType == null) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeType, nameof(enumType)); return InternalBoxEnum(rtType, value); } @@ -1138,11 +1128,11 @@ namespace System if (enumType == null) throw new ArgumentNullException(nameof(enumType)); if (!enumType.IsEnum) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeEnum, nameof(enumType)); Contract.EndContractBlock(); RuntimeType rtType = enumType as RuntimeType; if (rtType == null) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeType, nameof(enumType)); return InternalBoxEnum(rtType, value); } @@ -1152,11 +1142,11 @@ namespace System if (enumType == null) throw new ArgumentNullException(nameof(enumType)); if (!enumType.IsEnum) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeEnum, nameof(enumType)); Contract.EndContractBlock(); RuntimeType rtType = enumType as RuntimeType; if (rtType == null) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeType, nameof(enumType)); return InternalBoxEnum(rtType, value); } @@ -1166,11 +1156,11 @@ namespace System if (enumType == null) throw new ArgumentNullException(nameof(enumType)); if (!enumType.IsEnum) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeEnum, nameof(enumType)); Contract.EndContractBlock(); RuntimeType rtType = enumType as RuntimeType; if (rtType == null) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeType, nameof(enumType)); return InternalBoxEnum(rtType, value); } @@ -1179,11 +1169,11 @@ namespace System if (enumType == null) throw new ArgumentNullException(nameof(enumType)); if (!enumType.IsEnum) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeEnum, nameof(enumType)); Contract.EndContractBlock(); RuntimeType rtType = enumType as RuntimeType; if (rtType == null) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeType, nameof(enumType)); return InternalBoxEnum(rtType, value); } @@ -1193,11 +1183,11 @@ namespace System if (enumType == null) throw new ArgumentNullException(nameof(enumType)); if (!enumType.IsEnum) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeEnum, nameof(enumType)); Contract.EndContractBlock(); RuntimeType rtType = enumType as RuntimeType; if (rtType == null) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeType, nameof(enumType)); return InternalBoxEnum(rtType, unchecked((long)value)); } @@ -1206,11 +1196,11 @@ namespace System if (enumType == null) throw new ArgumentNullException(nameof(enumType)); if (!enumType.IsEnum) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeEnum, nameof(enumType)); Contract.EndContractBlock(); RuntimeType rtType = enumType as RuntimeType; if (rtType == null) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeType, nameof(enumType)); return InternalBoxEnum(rtType, value); } @@ -1219,11 +1209,11 @@ namespace System if (enumType == null) throw new ArgumentNullException(nameof(enumType)); if (!enumType.IsEnum) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeEnum, nameof(enumType)); Contract.EndContractBlock(); RuntimeType rtType = enumType as RuntimeType; if (rtType == null) - throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType)); + throw new ArgumentException(SR.Arg_MustBeType, nameof(enumType)); return InternalBoxEnum(rtType, value ? 1 : 0); } #endregion |