summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Enum.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/mscorlib/src/System/Enum.cs')
-rw-r--r--src/mscorlib/src/System/Enum.cs174
1 files changed, 91 insertions, 83 deletions
diff --git a/src/mscorlib/src/System/Enum.cs b/src/mscorlib/src/System/Enum.cs
index a8104556e4..d39e005d48 100644
--- a/src/mscorlib/src/System/Enum.cs
+++ b/src/mscorlib/src/System/Enum.cs
@@ -9,6 +9,7 @@ using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
+using System.Diagnostics;
using System.Diagnostics.Contracts;
namespace System
@@ -23,7 +24,6 @@ namespace System
#endregion
#region Private Static Methods
- [System.Security.SecuritySafeCritical] // auto-generated
private static TypeValuesAndNames GetCachedValuesAndNames(RuntimeType enumType, bool getNames)
{
TypeValuesAndNames entry = enumType.GenericCache as TypeValuesAndNames;
@@ -47,7 +47,6 @@ namespace System
return entry;
}
- [System.Security.SecuritySafeCritical]
private unsafe String InternalFormattedHexString()
{
fixed (void* pValue = &JitHelpers.GetPinningHelper(this).m_data)
@@ -71,7 +70,7 @@ namespace System
case CorElementType.U8:
return (*(ulong*)pValue).ToString("X16", null);
default:
- Contract.Assert(false, "Invalid Object type in Format");
+ Debug.Assert(false, "Invalid Object type in Format");
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_UnknownEnumType"));
}
}
@@ -106,7 +105,7 @@ namespace System
return ((UInt64)(Int64)value).ToString("X16", null);
// All unsigned types will be directly cast
default:
- Contract.Assert(false, "Invalid Object type in Format");
+ Debug.Assert(false, "Invalid Object type in Format");
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_UnknownEnumType"));
}
}
@@ -157,7 +156,7 @@ namespace System
String[] names = entry.Names;
ulong[] values = entry.Values;
- Contract.Assert(names.Length == values.Length);
+ Debug.Assert(names.Length == values.Length);
int index = values.Length - 1;
StringBuilder retval = new StringBuilder();
@@ -245,27 +244,23 @@ namespace System
break;
// All unsigned types will be directly cast
default:
- Contract.Assert(false, "Invalid Object type in ToUInt64");
+ Debug.Assert(false, "Invalid Object type in ToUInt64");
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_UnknownEnumType"));
}
return result;
}
- [System.Security.SecurityCritical] // auto-generated
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern int InternalCompareTo(Object o1, Object o2);
- [System.Security.SecuritySafeCritical]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern RuntimeType InternalGetUnderlyingType(RuntimeType enumType);
- [System.Security.SecurityCritical] // auto-generated
[DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
[System.Security.SuppressUnmanagedCodeSecurity]
private static extern void GetEnumValuesAndNames(RuntimeTypeHandle enumType, ObjectHandleOnStack values, ObjectHandleOnStack names, bool getNames);
- [System.Security.SecurityCritical] // auto-generated
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private static extern Object InternalBoxEnum(RuntimeType enumType, long value);
#endregion
@@ -328,12 +323,28 @@ namespace System
return m_innerException;
default:
- Contract.Assert(false, "Unknown EnumParseFailure: " + m_failure);
+ Debug.Assert(false, "Unknown EnumParseFailure: " + m_failure);
return new ArgumentException(Environment.GetResourceString("Arg_EnumValueNotFound"));
}
}
}
+ public static bool TryParse(Type enumType, String value, out Object result)
+ {
+ return TryParse(enumType, value, false, out result);
+ }
+
+ public static bool TryParse(Type enumType, String value, bool ignoreCase, out Object result)
+ {
+ result = null;
+ EnumResult parseResult = new EnumResult();
+ bool retValue;
+
+ if (retValue = TryParseEnum(enumType, value, ignoreCase, ref parseResult))
+ result = parseResult.parsedEnum;
+ return retValue;
+ }
+
public static bool TryParse<TEnum>(String value, out TEnum result) where TEnum : struct
{
return TryParse(value, false, out result);
@@ -366,21 +377,35 @@ namespace System
throw parseResult.GetEnumParseException();
}
+ public static TEnum Parse<TEnum>(String value) where TEnum : struct
+ {
+ return Parse<TEnum>(value, false);
+ }
+
+ public static TEnum Parse<TEnum>(String value, bool ignoreCase) where TEnum : struct
+ {
+ EnumResult parseResult = new EnumResult() { canThrow = true };
+ if (TryParseEnum(typeof(TEnum), value, ignoreCase, ref parseResult))
+ return (TEnum)parseResult.parsedEnum;
+ else
+ throw parseResult.GetEnumParseException();
+ }
+
private static bool TryParseEnum(Type enumType, String value, bool ignoreCase, ref EnumResult parseResult)
{
if (enumType == null)
- throw new ArgumentNullException("enumType");
+ throw new ArgumentNullException(nameof(enumType));
Contract.EndContractBlock();
RuntimeType rtType = enumType as RuntimeType;
if (rtType == null)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType));
if (!enumType.IsEnum)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType));
if (value == null) {
- parseResult.SetFailure(ParseFailureKind.ArgumentNull, "value");
+ parseResult.SetFailure(ParseFailureKind.ArgumentNull, nameof(value));
return false;
}
@@ -504,7 +529,7 @@ namespace System
public static Type GetUnderlyingType(Type enumType)
{
if (enumType == null)
- throw new ArgumentNullException("enumType");
+ throw new ArgumentNullException(nameof(enumType));
Contract.Ensures(Contract.Result<Type>() != null);
Contract.EndContractBlock();
@@ -515,7 +540,7 @@ namespace System
public static Array GetValues(Type enumType)
{
if (enumType == null)
- throw new ArgumentNullException("enumType");
+ throw new ArgumentNullException(nameof(enumType));
Contract.Ensures(Contract.Result<Array>() != null);
Contract.EndContractBlock();
@@ -532,7 +557,7 @@ namespace System
public static String GetName(Type enumType, Object value)
{
if (enumType == null)
- throw new ArgumentNullException("enumType");
+ throw new ArgumentNullException(nameof(enumType));
Contract.EndContractBlock();
return enumType.GetEnumName(value);
@@ -542,7 +567,7 @@ namespace System
public static String[] GetNames(Type enumType)
{
if (enumType == null)
- throw new ArgumentNullException("enumType");
+ throw new ArgumentNullException(nameof(enumType));
Contract.Ensures(Contract.Result<String[]>() != null);
Contract.EndContractBlock();
@@ -559,7 +584,7 @@ namespace System
public static Object ToObject(Type enumType, Object value)
{
if (value == null)
- throw new ArgumentNullException("value");
+ throw new ArgumentNullException(nameof(value));
Contract.EndContractBlock();
// Delegate rest of error checking to the other functions
@@ -599,7 +624,7 @@ namespace System
default:
// All unsigned types will be directly cast
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnumBaseTypeOrEnum"), "value");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnumBaseTypeOrEnum"), nameof(value));
}
}
@@ -608,7 +633,7 @@ namespace System
public static bool IsDefined(Type enumType, Object value)
{
if (enumType == null)
- throw new ArgumentNullException("enumType");
+ throw new ArgumentNullException(nameof(enumType));
Contract.EndContractBlock();
return enumType.IsEnumDefined(value);
@@ -618,21 +643,21 @@ namespace System
public static String Format(Type enumType, Object value, String format)
{
if (enumType == null)
- throw new ArgumentNullException("enumType");
+ throw new ArgumentNullException(nameof(enumType));
if (!enumType.IsEnum)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType));
if (value == null)
- throw new ArgumentNullException("value");
+ throw new ArgumentNullException(nameof(value));
if (format == null)
- throw new ArgumentNullException("format");
+ throw new ArgumentNullException(nameof(format));
Contract.EndContractBlock();
RuntimeType rtType = enumType as RuntimeType;
if (rtType == null)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType));
// Check if both of them are of the same type
Type valueType = value.GetType();
@@ -698,7 +723,6 @@ namespace System
#endregion
#region Private Methods
- [System.Security.SecuritySafeCritical]
internal unsafe Object GetValue()
{
fixed (void* pValue = &JitHelpers.GetPinningHelper(this).m_data)
@@ -734,13 +758,12 @@ namespace System
case CorElementType.U:
return *(UIntPtr*)pValue;
default:
- Contract.Assert(false, "Invalid primitive type");
+ Debug.Assert(false, "Invalid primitive type");
return null;
}
}
}
- [System.Security.SecuritySafeCritical]
private unsafe ulong ToUInt64()
{
fixed (void* pValue = &JitHelpers.GetPinningHelper(this).m_data)
@@ -787,32 +810,29 @@ namespace System
return *(uint*)pValue;
}
default:
- Contract.Assert(false, "Invalid primitive type");
+ Debug.Assert(false, "Invalid primitive type");
return 0;
}
}
}
- [System.Security.SecurityCritical] // auto-generated
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern bool InternalHasFlag(Enum flags);
- [System.Security.SecuritySafeCritical] // auto-generated
[MethodImplAttribute(MethodImplOptions.InternalCall)]
private extern CorElementType InternalGetCorElementType();
#endregion
#region Object Overrides
- [System.Security.SecuritySafeCritical] // auto-generated
[MethodImplAttribute(MethodImplOptions.InternalCall)]
public extern override bool Equals(Object obj);
- [System.Security.SecuritySafeCritical]
public override unsafe int GetHashCode()
{
- // Avoid boxing by inlining GetValue()
- // return GetValue().GetHashCode();
+ // CONTRACT with the runtime: GetHashCode of enum types is implemented as GetHashCode of the underlying type.
+ // The runtime can bypass calls to Enum::GetHashCode and call the underlying type's GetHashCode directly
+ // to avoid boxing the enum.
fixed (void* pValue = &JitHelpers.GetPinningHelper(this).m_data)
{
@@ -847,7 +867,7 @@ namespace System
case CorElementType.U:
return (*(UIntPtr*)pValue).GetHashCode();
default:
- Contract.Assert(false, "Invalid primitive type");
+ Debug.Assert(false, "Invalid primitive type");
return 0;
}
}
@@ -876,7 +896,6 @@ namespace System
#endregion
#region IComparable
- [System.Security.SecuritySafeCritical] // auto-generated
public int CompareTo(Object target)
{
const int retIncompatibleMethodTables = 2; // indicates that the method tables did not match
@@ -904,7 +923,7 @@ namespace System
else
{
// assert valid return code (3)
- Contract.Assert(ret == retInvalidEnumType, "Enum.InternalCompareTo return code was invalid");
+ Debug.Assert(ret == retInvalidEnumType, "Enum.InternalCompareTo return code was invalid");
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_UnknownEnumType"));
}
@@ -942,10 +961,9 @@ namespace System
return ToString();
}
- [System.Security.SecuritySafeCritical]
public Boolean HasFlag(Enum flag) {
if (flag == null)
- throw new ArgumentNullException("flag");
+ throw new ArgumentNullException(nameof(flag));
Contract.EndContractBlock();
if (!this.GetType().IsEquivalentTo(flag.GetType())) {
@@ -1013,7 +1031,7 @@ namespace System
return TypeCode.Char;
}
- Contract.Assert(false, "Unknown underlying type.");
+ Debug.Assert(false, "Unknown underlying type.");
throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_UnknownEnumType"));
}
@@ -1109,155 +1127,145 @@ namespace System
#endregion
#region ToObject
- [System.Security.SecuritySafeCritical] // auto-generated
[CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(true)]
public static Object ToObject(Type enumType, sbyte value)
{
if (enumType == null)
- throw new ArgumentNullException("enumType");
+ throw new ArgumentNullException(nameof(enumType));
if (!enumType.IsEnum)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType));
Contract.EndContractBlock();
RuntimeType rtType = enumType as RuntimeType;
if (rtType == null)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType));
return InternalBoxEnum(rtType, value);
}
- [System.Security.SecuritySafeCritical] // auto-generated
[System.Runtime.InteropServices.ComVisible(true)]
public static Object ToObject(Type enumType, short value)
{
if (enumType == null)
- throw new ArgumentNullException("enumType");
+ throw new ArgumentNullException(nameof(enumType));
if (!enumType.IsEnum)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType));
Contract.EndContractBlock();
RuntimeType rtType = enumType as RuntimeType;
if (rtType == null)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType));
return InternalBoxEnum(rtType, value);
}
- [System.Security.SecuritySafeCritical] // auto-generated
[System.Runtime.InteropServices.ComVisible(true)]
public static Object ToObject(Type enumType, int value)
{
if (enumType == null)
- throw new ArgumentNullException("enumType");
+ throw new ArgumentNullException(nameof(enumType));
if (!enumType.IsEnum)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType));
Contract.EndContractBlock();
RuntimeType rtType = enumType as RuntimeType;
if (rtType == null)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType));
return InternalBoxEnum(rtType, value);
}
- [System.Security.SecuritySafeCritical] // auto-generated
[System.Runtime.InteropServices.ComVisible(true)]
public static Object ToObject(Type enumType, byte value)
{
if (enumType == null)
- throw new ArgumentNullException("enumType");
+ throw new ArgumentNullException(nameof(enumType));
if (!enumType.IsEnum)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType));
Contract.EndContractBlock();
RuntimeType rtType = enumType as RuntimeType;
if (rtType == null)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType));
return InternalBoxEnum(rtType, value);
}
- [System.Security.SecuritySafeCritical] // auto-generated
[CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(true)]
public static Object ToObject(Type enumType, ushort value)
{
if (enumType == null)
- throw new ArgumentNullException("enumType");
+ throw new ArgumentNullException(nameof(enumType));
if (!enumType.IsEnum)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType));
Contract.EndContractBlock();
RuntimeType rtType = enumType as RuntimeType;
if (rtType == null)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType));
return InternalBoxEnum(rtType, value);
}
- [System.Security.SecuritySafeCritical] // auto-generated
[CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(true)]
public static Object ToObject(Type enumType, uint value)
{
if (enumType == null)
- throw new ArgumentNullException("enumType");
+ throw new ArgumentNullException(nameof(enumType));
if (!enumType.IsEnum)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType));
Contract.EndContractBlock();
RuntimeType rtType = enumType as RuntimeType;
if (rtType == null)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType));
return InternalBoxEnum(rtType, value);
}
- [System.Security.SecuritySafeCritical] // auto-generated
[System.Runtime.InteropServices.ComVisible(true)]
public static Object ToObject(Type enumType, long value)
{
if (enumType == null)
- throw new ArgumentNullException("enumType");
+ throw new ArgumentNullException(nameof(enumType));
if (!enumType.IsEnum)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType));
Contract.EndContractBlock();
RuntimeType rtType = enumType as RuntimeType;
if (rtType == null)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType));
return InternalBoxEnum(rtType, value);
}
- [System.Security.SecuritySafeCritical] // auto-generated
[CLSCompliant(false)]
[System.Runtime.InteropServices.ComVisible(true)]
public static Object ToObject(Type enumType, ulong value)
{
if (enumType == null)
- throw new ArgumentNullException("enumType");
+ throw new ArgumentNullException(nameof(enumType));
if (!enumType.IsEnum)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType));
Contract.EndContractBlock();
RuntimeType rtType = enumType as RuntimeType;
if (rtType == null)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType));
return InternalBoxEnum(rtType, unchecked((long)value));
}
- [System.Security.SecuritySafeCritical] // auto-generated
private static Object ToObject(Type enumType, char value)
{
if (enumType == null)
- throw new ArgumentNullException("enumType");
+ throw new ArgumentNullException(nameof(enumType));
if (!enumType.IsEnum)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType));
Contract.EndContractBlock();
RuntimeType rtType = enumType as RuntimeType;
if (rtType == null)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType));
return InternalBoxEnum(rtType, value);
}
- [System.Security.SecuritySafeCritical] // auto-generated
private static Object ToObject(Type enumType, bool value)
{
if (enumType == null)
- throw new ArgumentNullException("enumType");
+ throw new ArgumentNullException(nameof(enumType));
if (!enumType.IsEnum)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), nameof(enumType));
Contract.EndContractBlock();
RuntimeType rtType = enumType as RuntimeType;
if (rtType == null)
- throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");
+ throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), nameof(enumType));
return InternalBoxEnum(rtType, value ? 1 : 0);
}
#endregion