Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
Enum.cs
Go to the documentation of this file.
8
9namespace System;
10
12[TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
14{
15 internal sealed class EnumInfo
16 {
17 public readonly bool HasFlagsAttribute;
18
19 public readonly ulong[] Values;
20
21 public readonly string[] Names;
22
23 public EnumInfo(bool hasFlagsAttribute, ulong[] values, string[] names)
24 {
25 HasFlagsAttribute = hasFlagsAttribute;
26 Values = values;
27 Names = names;
28 }
29 }
30
31 private const char EnumSeparatorChar = ',';
32
33 [DllImport("QCall", CharSet = CharSet.Unicode)]
34 private static extern void GetEnumValuesAndNames(QCallTypeHandle enumType, ObjectHandleOnStack values, ObjectHandleOnStack names, Interop.BOOL getNames);
35
36 [MethodImpl(MethodImplOptions.InternalCall)]
37 public override extern bool Equals([NotNullWhen(true)] object? obj);
38
39 [MethodImpl(MethodImplOptions.InternalCall)]
40 private static extern object InternalBoxEnum(RuntimeType enumType, long value);
41
42 [MethodImpl(MethodImplOptions.InternalCall)]
44
45 [MethodImpl(MethodImplOptions.InternalCall)]
46 internal static extern RuntimeType InternalGetUnderlyingType(RuntimeType enumType);
47
48 [MethodImpl(MethodImplOptions.InternalCall)]
49 private extern bool InternalHasFlag(Enum flags);
50
51 private static EnumInfo GetEnumInfo(RuntimeType enumType, bool getNames = true)
52 {
53 EnumInfo enumInfo = enumType.GenericCache as EnumInfo;
54 if (enumInfo == null || (getNames && enumInfo.Names == null))
55 {
56 ulong[] o = null;
57 string[] o2 = null;
59 GetEnumValuesAndNames(new QCallTypeHandle(ref rth), ObjectHandleOnStack.Create(ref o), ObjectHandleOnStack.Create(ref o2), getNames ? Interop.BOOL.TRUE : Interop.BOOL.FALSE);
60 bool hasFlagsAttribute = enumType.IsDefined(typeof(FlagsAttribute), inherit: false);
61 enumInfo = (EnumInfo)(enumType.GenericCache = new EnumInfo(hasFlagsAttribute, o, o2));
62 }
63 return enumInfo;
64 }
65
66 private string ValueToString()
67 {
68 ref byte rawData = ref this.GetRawData();
69 return InternalGetCorElementType() switch
70 {
71 CorElementType.ELEMENT_TYPE_I1 => Unsafe.As<byte, sbyte>(ref rawData).ToString(),
72 CorElementType.ELEMENT_TYPE_U1 => rawData.ToString(),
73 CorElementType.ELEMENT_TYPE_BOOLEAN => Unsafe.As<byte, bool>(ref rawData).ToString(),
74 CorElementType.ELEMENT_TYPE_I2 => Unsafe.As<byte, short>(ref rawData).ToString(),
75 CorElementType.ELEMENT_TYPE_U2 => Unsafe.As<byte, ushort>(ref rawData).ToString(),
76 CorElementType.ELEMENT_TYPE_CHAR => Unsafe.As<byte, char>(ref rawData).ToString(),
77 CorElementType.ELEMENT_TYPE_I4 => Unsafe.As<byte, int>(ref rawData).ToString(),
78 CorElementType.ELEMENT_TYPE_U4 => Unsafe.As<byte, uint>(ref rawData).ToString(),
79 CorElementType.ELEMENT_TYPE_R4 => Unsafe.As<byte, float>(ref rawData).ToString(),
80 CorElementType.ELEMENT_TYPE_I8 => Unsafe.As<byte, long>(ref rawData).ToString(),
81 CorElementType.ELEMENT_TYPE_U8 => Unsafe.As<byte, ulong>(ref rawData).ToString(),
82 CorElementType.ELEMENT_TYPE_R8 => Unsafe.As<byte, double>(ref rawData).ToString(),
83 CorElementType.ELEMENT_TYPE_I => Unsafe.As<byte, IntPtr>(ref rawData).ToString(),
84 CorElementType.ELEMENT_TYPE_U => Unsafe.As<byte, UIntPtr>(ref rawData).ToString(),
86 };
87 }
88
89 private string ValueToHexString()
90 {
91 ref byte rawData = ref this.GetRawData();
92 Span<byte> destination = stackalloc byte[8];
93 int length;
95 {
96 case CorElementType.ELEMENT_TYPE_I1:
97 case CorElementType.ELEMENT_TYPE_U1:
98 destination[0] = rawData;
99 length = 1;
100 break;
101 case CorElementType.ELEMENT_TYPE_BOOLEAN:
102 if (rawData == 0)
103 {
104 return "00";
105 }
106 return "01";
107 case CorElementType.ELEMENT_TYPE_CHAR:
108 case CorElementType.ELEMENT_TYPE_I2:
109 case CorElementType.ELEMENT_TYPE_U2:
110 BinaryPrimitives.WriteUInt16BigEndian(destination, Unsafe.As<byte, ushort>(ref rawData));
111 length = 2;
112 break;
113 case CorElementType.ELEMENT_TYPE_I4:
114 case CorElementType.ELEMENT_TYPE_U4:
115 BinaryPrimitives.WriteUInt32BigEndian(destination, Unsafe.As<byte, uint>(ref rawData));
116 length = 4;
117 break;
118 case CorElementType.ELEMENT_TYPE_I8:
119 case CorElementType.ELEMENT_TYPE_U8:
120 BinaryPrimitives.WriteUInt64BigEndian(destination, Unsafe.As<byte, ulong>(ref rawData));
121 length = 8;
122 break;
123 default:
125 }
126 return HexConverter.ToString(destination.Slice(0, length));
127 }
128
129 private static string ValueToHexString(object value)
130 {
131 return Convert.GetTypeCode(value) switch
132 {
133 TypeCode.SByte => ((byte)(sbyte)value).ToString("X2", null),
134 TypeCode.Byte => ((byte)value).ToString("X2", null),
135 TypeCode.Boolean => ((bool)value) ? "01" : "00",
136 TypeCode.Int16 => ((ushort)(short)value).ToString("X4", null),
137 TypeCode.UInt16 => ((ushort)value).ToString("X4", null),
138 TypeCode.Char => ((ushort)(char)value).ToString("X4", null),
139 TypeCode.UInt32 => ((uint)value).ToString("X8", null),
140 TypeCode.Int32 => ((uint)(int)value).ToString("X8", null),
141 TypeCode.UInt64 => ((ulong)value).ToString("X16", null),
142 TypeCode.Int64 => ((ulong)(long)value).ToString("X16", null),
144 };
145 }
146
147 internal static string GetEnumName(RuntimeType enumType, ulong ulValue)
148 {
149 return GetEnumName(GetEnumInfo(enumType), ulValue);
150 }
151
152 private static string GetEnumName(EnumInfo enumInfo, ulong ulValue)
153 {
154 int num = Array.BinarySearch(enumInfo.Values, ulValue);
155 if (num >= 0)
156 {
157 return enumInfo.Names[num];
158 }
159 return null;
160 }
161
162 private static string InternalFormat(RuntimeType enumType, ulong value)
163 {
164 EnumInfo enumInfo = GetEnumInfo(enumType);
165 if (!enumInfo.HasFlagsAttribute)
166 {
167 return GetEnumName(enumInfo, value);
168 }
169 return InternalFlagsFormat(enumInfo, value);
170 }
171
172 private static string InternalFlagsFormat(RuntimeType enumType, ulong result)
173 {
174 return InternalFlagsFormat(GetEnumInfo(enumType), result);
175 }
176
177 private static string InternalFlagsFormat(EnumInfo enumInfo, ulong resultValue)
178 {
179 string[] names = enumInfo.Names;
180 ulong[] values = enumInfo.Values;
181 if (resultValue == 0L)
182 {
183 if (values.Length == 0 || values[0] != 0L)
184 {
185 return "0";
186 }
187 return names[0];
188 }
189 Span<int> span = stackalloc int[64];
190 int num;
191 for (num = values.Length - 1; num >= 0; num--)
192 {
193 if (values[num] == resultValue)
194 {
195 return names[num];
196 }
197 if (values[num] < resultValue)
198 {
199 break;
200 }
201 }
202 int num2 = 0;
203 int num3 = 0;
204 while (num >= 0)
205 {
206 ulong num4 = values[num];
207 if (num == 0 && num4 == 0L)
208 {
209 break;
210 }
211 if ((resultValue & num4) == num4)
212 {
213 resultValue -= num4;
214 span[num3++] = num;
215 num2 = checked(num2 + names[num].Length);
216 }
217 num--;
218 }
219 if (resultValue != 0L)
220 {
221 return null;
222 }
223 string text = string.FastAllocateString(checked(num2 + 2 * (num3 - 1)));
224 Span<char> destination = new Span<char>(ref text.GetRawStringData(), text.Length);
225 string text2 = names[span[--num3]];
226 text2.CopyTo(destination);
227 destination = destination.Slice(text2.Length);
228 while (--num3 >= 0)
229 {
230 destination[0] = ',';
231 destination[1] = ' ';
232 destination = destination.Slice(2);
233 text2 = names[span[num3]];
234 text2.CopyTo(destination);
235 destination = destination.Slice(text2.Length);
236 }
237 return text;
238 }
239
240 internal static ulong ToUInt64(object value)
241 {
242 return Convert.GetTypeCode(value) switch
243 {
244 TypeCode.SByte => (ulong)(sbyte)value,
245 TypeCode.Byte => (byte)value,
246 TypeCode.Boolean => (ulong)(((bool)value) ? 1 : 0),
247 TypeCode.Int16 => (ulong)(short)value,
248 TypeCode.UInt16 => (ushort)value,
249 TypeCode.Char => (char)value,
250 TypeCode.UInt32 => (uint)value,
251 TypeCode.Int32 => (ulong)(int)value,
252 TypeCode.UInt64 => (ulong)value,
253 TypeCode.Int64 => (ulong)(long)value,
255 };
256 }
257
258 private static ulong ToUInt64<TEnum>(TEnum value) where TEnum : struct, Enum
259 {
260 return Type.GetTypeCode(typeof(TEnum)) switch
261 {
262 TypeCode.SByte => (ulong)Unsafe.As<TEnum, sbyte>(ref value),
263 TypeCode.Byte => Unsafe.As<TEnum, byte>(ref value),
264 TypeCode.Boolean => (ulong)(Unsafe.As<TEnum, bool>(ref value) ? 1 : 0),
265 TypeCode.Int16 => (ulong)Unsafe.As<TEnum, short>(ref value),
266 TypeCode.UInt16 => Unsafe.As<TEnum, ushort>(ref value),
267 TypeCode.Char => Unsafe.As<TEnum, char>(ref value),
268 TypeCode.UInt32 => Unsafe.As<TEnum, uint>(ref value),
269 TypeCode.Int32 => (ulong)Unsafe.As<TEnum, int>(ref value),
270 TypeCode.UInt64 => Unsafe.As<TEnum, ulong>(ref value),
271 TypeCode.Int64 => (ulong)Unsafe.As<TEnum, long>(ref value),
273 };
274 }
275
276 public static string? GetName<TEnum>(TEnum value) where TEnum : struct, Enum
277 {
278 return GetEnumName((RuntimeType)typeof(TEnum), ToUInt64(value));
279 }
280
281 public static string? GetName(Type enumType, object value)
282 {
283 if ((object)enumType == null)
284 {
285 throw new ArgumentNullException("enumType");
286 }
287 return enumType.GetEnumName(value);
288 }
289
290 public static string[] GetNames<TEnum>() where TEnum : struct, Enum
291 {
292 return new ReadOnlySpan<string>(InternalGetNames((RuntimeType)typeof(TEnum))).ToArray();
293 }
294
295 public static string[] GetNames(Type enumType)
296 {
297 if ((object)enumType == null)
298 {
299 throw new ArgumentNullException("enumType");
300 }
301 return enumType.GetEnumNames();
302 }
303
304 internal static string[] InternalGetNames(RuntimeType enumType)
305 {
306 return GetEnumInfo(enumType).Names;
307 }
308
309 public static Type GetUnderlyingType(Type enumType)
310 {
311 if (enumType == null)
312 {
313 throw new ArgumentNullException("enumType");
314 }
315 return enumType.GetEnumUnderlyingType();
316 }
317
318 public static TEnum[] GetValues<TEnum>() where TEnum : struct, Enum
319 {
320 return (TEnum[])GetValues(typeof(TEnum));
321 }
322
323 public static Array GetValues(Type enumType)
324 {
325 if ((object)enumType == null)
326 {
327 throw new ArgumentNullException("enumType");
328 }
329 return enumType.GetEnumValues();
330 }
331
332 [Intrinsic]
333 public bool HasFlag(Enum flag)
334 {
335 if (flag == null)
336 {
337 throw new ArgumentNullException("flag");
338 }
339 if (!GetType().IsEquivalentTo(flag.GetType()))
340 {
341 throw new ArgumentException(SR.Format(SR.Argument_EnumTypeDoesNotMatch, flag.GetType(), GetType()));
342 }
343 return InternalHasFlag(flag);
344 }
345
346 internal static ulong[] InternalGetValues(RuntimeType enumType)
347 {
348 return GetEnumInfo(enumType, getNames: false).Values;
349 }
350
351 public static bool IsDefined<TEnum>(TEnum value) where TEnum : struct, Enum
352 {
353 RuntimeType enumType = (RuntimeType)typeof(TEnum);
354 ulong[] array = InternalGetValues(enumType);
355 ulong value2 = ToUInt64(value);
356 return Array.BinarySearch(array, value2) >= 0;
357 }
358
359 public static bool IsDefined(Type enumType, object value)
360 {
361 if ((object)enumType == null)
362 {
363 throw new ArgumentNullException("enumType");
364 }
365 return enumType.IsEnumDefined(value);
366 }
367
368 public static object Parse(Type enumType, string value)
369 {
370 return Parse(enumType, value, ignoreCase: false);
371 }
372
373 public static object Parse(Type enumType, ReadOnlySpan<char> value)
374 {
375 return Parse(enumType, value, ignoreCase: false);
376 }
377
378 public static object Parse(Type enumType, string value, bool ignoreCase)
379 {
380 object result;
381 bool flag = TryParse(enumType, value, ignoreCase, throwOnFailure: true, out result);
382 return result;
383 }
384
385 public static object Parse(Type enumType, ReadOnlySpan<char> value, bool ignoreCase)
386 {
387 object result;
388 bool flag = TryParse(enumType, value, ignoreCase, throwOnFailure: true, out result);
389 return result;
390 }
391
392 public static TEnum Parse<TEnum>(string value) where TEnum : struct
393 {
394 return Parse<TEnum>(value, ignoreCase: false);
395 }
396
397 public static TEnum Parse<TEnum>(ReadOnlySpan<char> value) where TEnum : struct
398 {
399 return Parse<TEnum>(value, ignoreCase: false);
400 }
401
402 public static TEnum Parse<TEnum>(string value, bool ignoreCase) where TEnum : struct
403 {
404 TEnum result;
405 bool flag = TryParse<TEnum>(value, ignoreCase, throwOnFailure: true, out result);
406 return result;
407 }
408
409 public static TEnum Parse<TEnum>(ReadOnlySpan<char> value, bool ignoreCase) where TEnum : struct
410 {
411 TEnum result;
412 bool flag = TryParse<TEnum>(value, ignoreCase, throwOnFailure: true, out result);
413 return result;
414 }
415
416 public static bool TryParse(Type enumType, string? value, out object? result)
417 {
418 return TryParse(enumType, value, ignoreCase: false, out result);
419 }
420
421 public static bool TryParse(Type enumType, ReadOnlySpan<char> value, out object? result)
422 {
423 return TryParse(enumType, value, ignoreCase: false, out result);
424 }
425
426 public static bool TryParse(Type enumType, string? value, bool ignoreCase, out object? result)
427 {
428 return TryParse(enumType, value, ignoreCase, throwOnFailure: false, out result);
429 }
430
431 public static bool TryParse(Type enumType, ReadOnlySpan<char> value, bool ignoreCase, out object? result)
432 {
433 return TryParse(enumType, value, ignoreCase, throwOnFailure: false, out result);
434 }
435
436 private static bool TryParse(Type enumType, string value, bool ignoreCase, bool throwOnFailure, out object result)
437 {
438 if (value == null)
439 {
440 if (throwOnFailure)
441 {
442 throw new ArgumentNullException("value");
443 }
444 result = null;
445 return false;
446 }
447 return TryParse(enumType, value.AsSpan(), ignoreCase, throwOnFailure, out result);
448 }
449
450 private static bool TryParse(Type enumType, ReadOnlySpan<char> value, bool ignoreCase, bool throwOnFailure, out object result)
451 {
452 RuntimeType runtimeType = ValidateRuntimeType(enumType);
453 value = value.TrimStart();
454 if (value.Length == 0)
455 {
456 if (throwOnFailure)
457 {
458 throw new ArgumentException(SR.Arg_MustContainEnumInfo, "value");
459 }
460 result = null;
461 return false;
462 }
463 int result3;
464 uint result4;
465 switch (Type.GetTypeCode(runtimeType))
466 {
467 case TypeCode.SByte:
468 {
469 bool flag = TryParseInt32Enum(runtimeType, value, -128, 127, ignoreCase, throwOnFailure, TypeCode.SByte, out result3);
470 result = (flag ? InternalBoxEnum(runtimeType, result3) : null);
471 return flag;
472 }
473 case TypeCode.Int16:
474 {
475 bool flag = TryParseInt32Enum(runtimeType, value, -32768, 32767, ignoreCase, throwOnFailure, TypeCode.Int16, out result3);
476 result = (flag ? InternalBoxEnum(runtimeType, result3) : null);
477 return flag;
478 }
479 case TypeCode.Int32:
480 {
481 bool flag = TryParseInt32Enum(runtimeType, value, int.MinValue, int.MaxValue, ignoreCase, throwOnFailure, TypeCode.Int32, out result3);
482 result = (flag ? InternalBoxEnum(runtimeType, result3) : null);
483 return flag;
484 }
485 case TypeCode.Byte:
486 {
487 bool flag = TryParseUInt32Enum(runtimeType, value, 255u, ignoreCase, throwOnFailure, TypeCode.Byte, out result4);
488 result = (flag ? InternalBoxEnum(runtimeType, result4) : null);
489 return flag;
490 }
491 case TypeCode.UInt16:
492 {
493 bool flag = TryParseUInt32Enum(runtimeType, value, 65535u, ignoreCase, throwOnFailure, TypeCode.UInt16, out result4);
494 result = (flag ? InternalBoxEnum(runtimeType, result4) : null);
495 return flag;
496 }
497 case TypeCode.UInt32:
498 {
499 bool flag = TryParseUInt32Enum(runtimeType, value, uint.MaxValue, ignoreCase, throwOnFailure, TypeCode.UInt32, out result4);
500 result = (flag ? InternalBoxEnum(runtimeType, result4) : null);
501 return flag;
502 }
503 case TypeCode.Int64:
504 {
505 long result5;
506 bool flag = TryParseInt64Enum(runtimeType, value, ignoreCase, throwOnFailure, out result5);
507 result = (flag ? InternalBoxEnum(runtimeType, result5) : null);
508 return flag;
509 }
510 case TypeCode.UInt64:
511 {
512 ulong result2;
513 bool flag = TryParseUInt64Enum(runtimeType, value, ignoreCase, throwOnFailure, out result2);
514 result = (flag ? InternalBoxEnum(runtimeType, (long)result2) : null);
515 return flag;
516 }
517 default:
518 return TryParseRareEnum(runtimeType, value, ignoreCase, throwOnFailure, out result);
519 }
520 }
521
522 public static bool TryParse<TEnum>([NotNullWhen(true)] string? value, out TEnum result) where TEnum : struct
523 {
524 return TryParse<TEnum>(value, ignoreCase: false, out result);
525 }
526
527 public static bool TryParse<TEnum>(ReadOnlySpan<char> value, out TEnum result) where TEnum : struct
528 {
529 return TryParse<TEnum>(value, ignoreCase: false, out result);
530 }
531
532 public static bool TryParse<TEnum>([NotNullWhen(true)] string? value, bool ignoreCase, out TEnum result) where TEnum : struct
533 {
534 return TryParse<TEnum>(value, ignoreCase, throwOnFailure: false, out result);
535 }
536
537 public static bool TryParse<TEnum>(ReadOnlySpan<char> value, bool ignoreCase, out TEnum result) where TEnum : struct
538 {
539 return TryParse<TEnum>(value, ignoreCase, throwOnFailure: false, out result);
540 }
541
542 private static bool TryParse<TEnum>(string value, bool ignoreCase, bool throwOnFailure, out TEnum result) where TEnum : struct
543 {
544 if (value == null)
545 {
546 if (throwOnFailure)
547 {
548 throw new ArgumentNullException("value");
549 }
550 result = default(TEnum);
551 return false;
552 }
553 return TryParse<TEnum>(value.AsSpan(), ignoreCase, throwOnFailure, out result);
554 }
555
556 private static bool TryParse<TEnum>(ReadOnlySpan<char> value, bool ignoreCase, bool throwOnFailure, out TEnum result) where TEnum : struct
557 {
558 if (!typeof(TEnum).IsEnum)
559 {
560 throw new ArgumentException(SR.Arg_MustBeEnum, "TEnum");
561 }
562 value = value.TrimStart();
563 if (value.Length == 0)
564 {
565 if (throwOnFailure)
566 {
567 throw new ArgumentException(SR.Arg_MustContainEnumInfo, "value");
568 }
569 result = default(TEnum);
570 return false;
571 }
572 RuntimeType enumType = (RuntimeType)typeof(TEnum);
573 int result3;
574 uint result6;
575 switch (Type.GetTypeCode(typeof(TEnum)))
576 {
577 case TypeCode.SByte:
578 {
579 bool flag = TryParseInt32Enum(enumType, value, -128, 127, ignoreCase, throwOnFailure, TypeCode.SByte, out result3);
580 sbyte source2 = (sbyte)result3;
581 result = Unsafe.As<sbyte, TEnum>(ref source2);
582 return flag;
583 }
584 case TypeCode.Int16:
585 {
586 bool flag = TryParseInt32Enum(enumType, value, -32768, 32767, ignoreCase, throwOnFailure, TypeCode.Int16, out result3);
587 short source = (short)result3;
588 result = Unsafe.As<short, TEnum>(ref source);
589 return flag;
590 }
591 case TypeCode.Int32:
592 {
593 bool flag = TryParseInt32Enum(enumType, value, int.MinValue, int.MaxValue, ignoreCase, throwOnFailure, TypeCode.Int32, out result3);
594 result = Unsafe.As<int, TEnum>(ref result3);
595 return flag;
596 }
597 case TypeCode.Byte:
598 {
599 bool flag = TryParseUInt32Enum(enumType, value, 255u, ignoreCase, throwOnFailure, TypeCode.Byte, out result6);
600 byte source4 = (byte)result6;
601 result = Unsafe.As<byte, TEnum>(ref source4);
602 return flag;
603 }
604 case TypeCode.UInt16:
605 {
606 bool flag = TryParseUInt32Enum(enumType, value, 65535u, ignoreCase, throwOnFailure, TypeCode.UInt16, out result6);
607 ushort source3 = (ushort)result6;
608 result = Unsafe.As<ushort, TEnum>(ref source3);
609 return flag;
610 }
611 case TypeCode.UInt32:
612 {
613 bool flag = TryParseUInt32Enum(enumType, value, uint.MaxValue, ignoreCase, throwOnFailure, TypeCode.UInt32, out result6);
614 result = Unsafe.As<uint, TEnum>(ref result6);
615 return flag;
616 }
617 case TypeCode.Int64:
618 {
619 long result5;
620 bool flag = TryParseInt64Enum(enumType, value, ignoreCase, throwOnFailure, out result5);
621 result = Unsafe.As<long, TEnum>(ref result5);
622 return flag;
623 }
624 case TypeCode.UInt64:
625 {
626 ulong result4;
627 bool flag = TryParseUInt64Enum(enumType, value, ignoreCase, throwOnFailure, out result4);
628 result = Unsafe.As<ulong, TEnum>(ref result4);
629 return flag;
630 }
631 default:
632 {
633 object result2;
634 bool flag = TryParseRareEnum(enumType, value, ignoreCase, throwOnFailure, out result2);
635 result = (flag ? ((TEnum)result2) : default(TEnum));
636 return flag;
637 }
638 }
639 }
640
641 private static bool TryParseInt32Enum(RuntimeType enumType, ReadOnlySpan<char> value, int minInclusive, int maxInclusive, bool ignoreCase, bool throwOnFailure, TypeCode type, out int result)
642 {
643 Number.ParsingStatus parsingStatus = Number.ParsingStatus.OK;
644 if (StartsNumber(value[0]))
645 {
646 parsingStatus = Number.TryParseInt32IntegerStyle(value, NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture.NumberFormat, out result);
647 if (parsingStatus == Number.ParsingStatus.OK)
648 {
649 if ((uint)(result - minInclusive) <= (uint)(maxInclusive - minInclusive))
650 {
651 return true;
652 }
653 parsingStatus = Number.ParsingStatus.Overflow;
654 }
655 }
656 ulong result2;
657 if (parsingStatus == Number.ParsingStatus.Overflow)
658 {
659 if (throwOnFailure)
660 {
662 }
663 }
664 else if (TryParseByName(enumType, value, ignoreCase, throwOnFailure, out result2))
665 {
666 result = (int)result2;
667 return true;
668 }
669 result = 0;
670 return false;
671 }
672
673 private static bool TryParseUInt32Enum(RuntimeType enumType, ReadOnlySpan<char> value, uint maxInclusive, bool ignoreCase, bool throwOnFailure, TypeCode type, out uint result)
674 {
675 Number.ParsingStatus parsingStatus = Number.ParsingStatus.OK;
676 if (StartsNumber(value[0]))
677 {
678 parsingStatus = Number.TryParseUInt32IntegerStyle(value, NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture.NumberFormat, out result);
679 if (parsingStatus == Number.ParsingStatus.OK)
680 {
681 if (result <= maxInclusive)
682 {
683 return true;
684 }
685 parsingStatus = Number.ParsingStatus.Overflow;
686 }
687 }
688 ulong result2;
689 if (parsingStatus == Number.ParsingStatus.Overflow)
690 {
691 if (throwOnFailure)
692 {
694 }
695 }
696 else if (TryParseByName(enumType, value, ignoreCase, throwOnFailure, out result2))
697 {
698 result = (uint)result2;
699 return true;
700 }
701 result = 0u;
702 return false;
703 }
704
705 private static bool TryParseInt64Enum(RuntimeType enumType, ReadOnlySpan<char> value, bool ignoreCase, bool throwOnFailure, out long result)
706 {
707 Number.ParsingStatus parsingStatus = Number.ParsingStatus.OK;
708 if (StartsNumber(value[0]))
709 {
710 parsingStatus = Number.TryParseInt64IntegerStyle(value, NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture.NumberFormat, out result);
711 if (parsingStatus == Number.ParsingStatus.OK)
712 {
713 return true;
714 }
715 }
716 ulong result2;
717 if (parsingStatus == Number.ParsingStatus.Overflow)
718 {
719 if (throwOnFailure)
720 {
722 }
723 }
724 else if (TryParseByName(enumType, value, ignoreCase, throwOnFailure, out result2))
725 {
726 result = (long)result2;
727 return true;
728 }
729 result = 0L;
730 return false;
731 }
732
733 private static bool TryParseUInt64Enum(RuntimeType enumType, ReadOnlySpan<char> value, bool ignoreCase, bool throwOnFailure, out ulong result)
734 {
735 Number.ParsingStatus parsingStatus = Number.ParsingStatus.OK;
736 if (StartsNumber(value[0]))
737 {
738 parsingStatus = Number.TryParseUInt64IntegerStyle(value, NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign, CultureInfo.InvariantCulture.NumberFormat, out result);
739 if (parsingStatus == Number.ParsingStatus.OK)
740 {
741 return true;
742 }
743 }
744 if (parsingStatus == Number.ParsingStatus.Overflow)
745 {
746 if (throwOnFailure)
747 {
749 }
750 }
751 else if (TryParseByName(enumType, value, ignoreCase, throwOnFailure, out result))
752 {
753 return true;
754 }
755 result = 0uL;
756 return false;
757 }
758
759 private static bool TryParseRareEnum(RuntimeType enumType, ReadOnlySpan<char> value, bool ignoreCase, bool throwOnFailure, [NotNullWhen(true)] out object result)
760 {
761 if (StartsNumber(value[0]))
762 {
763 Type underlyingType = GetUnderlyingType(enumType);
764 try
765 {
766 result = ToObject(enumType, Convert.ChangeType(value.ToString(), underlyingType, CultureInfo.InvariantCulture));
767 return true;
768 }
769 catch (FormatException)
770 {
771 }
772 catch when (!throwOnFailure)
773 {
774 result = null;
775 return false;
776 }
777 }
778 if (TryParseByName(enumType, value, ignoreCase, throwOnFailure, out var result2))
779 {
780 try
781 {
782 result = ToObject(enumType, result2);
783 return true;
784 }
785 catch when (!throwOnFailure)
786 {
787 }
788 }
789 result = null;
790 return false;
791 }
792
793 private static bool TryParseByName(RuntimeType enumType, ReadOnlySpan<char> value, bool ignoreCase, bool throwOnFailure, out ulong result)
794 {
795 ReadOnlySpan<char> readOnlySpan = value;
796 EnumInfo enumInfo = GetEnumInfo(enumType);
797 string[] names = enumInfo.Names;
798 ulong[] values = enumInfo.Values;
799 bool flag = true;
800 ulong num = 0uL;
801 while (value.Length > 0)
802 {
803 int num2 = value.IndexOf(',');
805 if (num2 == -1)
806 {
807 span = value.Trim();
808 value = default(ReadOnlySpan<char>);
809 }
810 else
811 {
812 if (num2 == value.Length - 1)
813 {
814 flag = false;
815 break;
816 }
817 span = value.Slice(0, num2).Trim();
818 value = value.Slice(num2 + 1);
819 }
820 bool flag2 = false;
821 if (ignoreCase)
822 {
823 for (int i = 0; i < names.Length; i++)
824 {
825 if (span.EqualsOrdinalIgnoreCase(names[i]))
826 {
827 num |= values[i];
828 flag2 = true;
829 break;
830 }
831 }
832 }
833 else
834 {
835 for (int j = 0; j < names.Length; j++)
836 {
837 if (span.EqualsOrdinal(names[j]))
838 {
839 num |= values[j];
840 flag2 = true;
841 break;
842 }
843 }
844 }
845 if (!flag2)
846 {
847 flag = false;
848 break;
849 }
850 }
851 if (flag)
852 {
853 result = num;
854 return true;
855 }
856 if (throwOnFailure)
857 {
858 throw new ArgumentException(SR.Format(SR.Arg_EnumValueNotFound, readOnlySpan.ToString()));
859 }
860 result = 0uL;
861 return false;
862 }
863
864 [MethodImpl(MethodImplOptions.AggressiveInlining)]
865 private static bool StartsNumber(char c)
866 {
867 if (!char.IsInRange(c, '0', '9') && c != '-')
868 {
869 return c == '+';
870 }
871 return true;
872 }
873
874 public static object ToObject(Type enumType, object value)
875 {
876 if (value == null)
877 {
878 throw new ArgumentNullException("value");
879 }
880 return Convert.GetTypeCode(value) switch
881 {
882 TypeCode.Int32 => ToObject(enumType, (int)value),
883 TypeCode.SByte => ToObject(enumType, (sbyte)value),
884 TypeCode.Int16 => ToObject(enumType, (short)value),
885 TypeCode.Int64 => ToObject(enumType, (long)value),
886 TypeCode.UInt32 => ToObject(enumType, (uint)value),
887 TypeCode.Byte => ToObject(enumType, (byte)value),
888 TypeCode.UInt16 => ToObject(enumType, (ushort)value),
889 TypeCode.UInt64 => ToObject(enumType, (ulong)value),
890 TypeCode.Char => ToObject(enumType, (char)value),
891 TypeCode.Boolean => ToObject(enumType, (bool)value),
892 _ => throw new ArgumentException(SR.Arg_MustBeEnumBaseTypeOrEnum, "value"),
893 };
894 }
895
896 public static string Format(Type enumType, object value, string format)
897 {
898 RuntimeType enumType2 = ValidateRuntimeType(enumType);
899 if (value == null)
900 {
901 throw new ArgumentNullException("value");
902 }
903 if (format == null)
904 {
905 throw new ArgumentNullException("format");
906 }
908 if (type.IsEnum)
909 {
910 if (!type.IsEquivalentTo(enumType))
911 {
913 }
914 if (format.Length != 1)
915 {
917 }
918 return ((Enum)value).ToString(format);
919 }
920 Type underlyingType = GetUnderlyingType(enumType);
921 if (type != underlyingType)
922 {
924 }
925 if (format.Length == 1)
926 {
927 switch (format[0])
928 {
929 case 'G':
930 case 'g':
931 return InternalFormat(enumType2, ToUInt64(value)) ?? value.ToString();
932 case 'D':
933 case 'd':
934 return value.ToString();
935 case 'X':
936 case 'x':
937 return ValueToHexString(value);
938 case 'F':
939 case 'f':
940 return InternalFlagsFormat(enumType2, ToUInt64(value)) ?? value.ToString();
941 }
942 }
944 }
945
946 internal object GetValue()
947 {
948 ref byte rawData = ref this.GetRawData();
949 return InternalGetCorElementType() switch
950 {
951 CorElementType.ELEMENT_TYPE_I1 => Unsafe.As<byte, sbyte>(ref rawData),
952 CorElementType.ELEMENT_TYPE_U1 => rawData,
953 CorElementType.ELEMENT_TYPE_BOOLEAN => Unsafe.As<byte, bool>(ref rawData),
954 CorElementType.ELEMENT_TYPE_I2 => Unsafe.As<byte, short>(ref rawData),
955 CorElementType.ELEMENT_TYPE_U2 => Unsafe.As<byte, ushort>(ref rawData),
956 CorElementType.ELEMENT_TYPE_CHAR => Unsafe.As<byte, char>(ref rawData),
957 CorElementType.ELEMENT_TYPE_I4 => Unsafe.As<byte, int>(ref rawData),
958 CorElementType.ELEMENT_TYPE_U4 => Unsafe.As<byte, uint>(ref rawData),
959 CorElementType.ELEMENT_TYPE_R4 => Unsafe.As<byte, float>(ref rawData),
960 CorElementType.ELEMENT_TYPE_I8 => Unsafe.As<byte, long>(ref rawData),
961 CorElementType.ELEMENT_TYPE_U8 => Unsafe.As<byte, ulong>(ref rawData),
962 CorElementType.ELEMENT_TYPE_R8 => Unsafe.As<byte, double>(ref rawData),
963 CorElementType.ELEMENT_TYPE_I => Unsafe.As<byte, IntPtr>(ref rawData),
964 CorElementType.ELEMENT_TYPE_U => Unsafe.As<byte, UIntPtr>(ref rawData),
966 };
967 }
968
969 private ulong ToUInt64()
970 {
971 ref byte rawData = ref this.GetRawData();
973 {
974 case CorElementType.ELEMENT_TYPE_I1:
975 return (ulong)Unsafe.As<byte, sbyte>(ref rawData);
976 case CorElementType.ELEMENT_TYPE_U1:
977 return rawData;
978 case CorElementType.ELEMENT_TYPE_BOOLEAN:
979 if (rawData == 0)
980 {
981 return 0uL;
982 }
983 return 1uL;
984 case CorElementType.ELEMENT_TYPE_I2:
985 return (ulong)Unsafe.As<byte, short>(ref rawData);
986 case CorElementType.ELEMENT_TYPE_CHAR:
987 case CorElementType.ELEMENT_TYPE_U2:
988 return Unsafe.As<byte, ushort>(ref rawData);
989 case CorElementType.ELEMENT_TYPE_I4:
990 return (ulong)Unsafe.As<byte, int>(ref rawData);
991 case CorElementType.ELEMENT_TYPE_U4:
992 case CorElementType.ELEMENT_TYPE_R4:
993 return Unsafe.As<byte, uint>(ref rawData);
994 case CorElementType.ELEMENT_TYPE_I8:
995 return (ulong)Unsafe.As<byte, long>(ref rawData);
996 case CorElementType.ELEMENT_TYPE_U8:
997 case CorElementType.ELEMENT_TYPE_R8:
998 return Unsafe.As<byte, ulong>(ref rawData);
999 case CorElementType.ELEMENT_TYPE_I:
1000 return (ulong)(long)Unsafe.As<byte, IntPtr>(ref rawData);
1001 case CorElementType.ELEMENT_TYPE_U:
1002 return (ulong)Unsafe.As<byte, UIntPtr>(ref rawData);
1003 default:
1005 }
1006 }
1007
1008 public override int GetHashCode()
1009 {
1010 ref byte rawData = ref this.GetRawData();
1011 return InternalGetCorElementType() switch
1012 {
1013 CorElementType.ELEMENT_TYPE_I1 => Unsafe.As<byte, sbyte>(ref rawData).GetHashCode(),
1014 CorElementType.ELEMENT_TYPE_U1 => rawData.GetHashCode(),
1015 CorElementType.ELEMENT_TYPE_BOOLEAN => Unsafe.As<byte, bool>(ref rawData).GetHashCode(),
1016 CorElementType.ELEMENT_TYPE_I2 => Unsafe.As<byte, short>(ref rawData).GetHashCode(),
1017 CorElementType.ELEMENT_TYPE_U2 => Unsafe.As<byte, ushort>(ref rawData).GetHashCode(),
1018 CorElementType.ELEMENT_TYPE_CHAR => Unsafe.As<byte, char>(ref rawData).GetHashCode(),
1019 CorElementType.ELEMENT_TYPE_I4 => Unsafe.As<byte, int>(ref rawData).GetHashCode(),
1020 CorElementType.ELEMENT_TYPE_U4 => Unsafe.As<byte, uint>(ref rawData).GetHashCode(),
1021 CorElementType.ELEMENT_TYPE_R4 => Unsafe.As<byte, float>(ref rawData).GetHashCode(),
1022 CorElementType.ELEMENT_TYPE_I8 => Unsafe.As<byte, long>(ref rawData).GetHashCode(),
1023 CorElementType.ELEMENT_TYPE_U8 => Unsafe.As<byte, ulong>(ref rawData).GetHashCode(),
1024 CorElementType.ELEMENT_TYPE_R8 => Unsafe.As<byte, double>(ref rawData).GetHashCode(),
1025 CorElementType.ELEMENT_TYPE_I => Unsafe.As<byte, IntPtr>(ref rawData).GetHashCode(),
1026 CorElementType.ELEMENT_TYPE_U => Unsafe.As<byte, UIntPtr>(ref rawData).GetHashCode(),
1028 };
1029 }
1030
1031 public override string ToString()
1032 {
1033 return InternalFormat((RuntimeType)GetType(), ToUInt64()) ?? ValueToString();
1034 }
1035
1036 public int CompareTo(object? target)
1037 {
1038 if (target == this)
1039 {
1040 return 0;
1041 }
1042 if (target == null)
1043 {
1044 return 1;
1045 }
1046 if (GetType() != target.GetType())
1047 {
1048 throw new ArgumentException(SR.Format(SR.Arg_EnumAndObjectMustBeSameType, target.GetType(), GetType()));
1049 }
1050 ref byte rawData = ref this.GetRawData();
1051 ref byte rawData2 = ref target.GetRawData();
1052 switch (InternalGetCorElementType())
1053 {
1054 case CorElementType.ELEMENT_TYPE_I1:
1055 return Unsafe.As<byte, sbyte>(ref rawData).CompareTo(Unsafe.As<byte, sbyte>(ref rawData2));
1056 case CorElementType.ELEMENT_TYPE_BOOLEAN:
1057 case CorElementType.ELEMENT_TYPE_U1:
1058 return rawData.CompareTo(rawData2);
1059 case CorElementType.ELEMENT_TYPE_I2:
1060 return Unsafe.As<byte, short>(ref rawData).CompareTo(Unsafe.As<byte, short>(ref rawData2));
1061 case CorElementType.ELEMENT_TYPE_CHAR:
1062 case CorElementType.ELEMENT_TYPE_U2:
1063 return Unsafe.As<byte, ushort>(ref rawData).CompareTo(Unsafe.As<byte, ushort>(ref rawData2));
1064 case CorElementType.ELEMENT_TYPE_I4:
1065 return Unsafe.As<byte, int>(ref rawData).CompareTo(Unsafe.As<byte, int>(ref rawData2));
1066 case CorElementType.ELEMENT_TYPE_U4:
1067 return Unsafe.As<byte, uint>(ref rawData).CompareTo(Unsafe.As<byte, uint>(ref rawData2));
1068 case CorElementType.ELEMENT_TYPE_I8:
1069 case CorElementType.ELEMENT_TYPE_I:
1070 return Unsafe.As<byte, long>(ref rawData).CompareTo(Unsafe.As<byte, long>(ref rawData2));
1071 case CorElementType.ELEMENT_TYPE_U8:
1072 case CorElementType.ELEMENT_TYPE_U:
1073 return Unsafe.As<byte, ulong>(ref rawData).CompareTo(Unsafe.As<byte, ulong>(ref rawData2));
1074 case CorElementType.ELEMENT_TYPE_R4:
1075 return Unsafe.As<byte, float>(ref rawData).CompareTo(Unsafe.As<byte, float>(ref rawData2));
1076 case CorElementType.ELEMENT_TYPE_R8:
1077 return Unsafe.As<byte, double>(ref rawData).CompareTo(Unsafe.As<byte, double>(ref rawData2));
1078 default:
1080 }
1081 }
1082
1083 [Obsolete("The provider argument is not used. Use ToString(String) instead.")]
1084 public string ToString(string? format, IFormatProvider? provider)
1085 {
1086 return ToString(format);
1087 }
1088
1089 public string ToString(string? format)
1090 {
1091 if (string.IsNullOrEmpty(format))
1092 {
1093 return ToString();
1094 }
1095 if (format.Length == 1)
1096 {
1097 switch (format[0])
1098 {
1099 case 'G':
1100 case 'g':
1101 return ToString();
1102 case 'D':
1103 case 'd':
1104 return ValueToString();
1105 case 'X':
1106 case 'x':
1107 return ValueToHexString();
1108 case 'F':
1109 case 'f':
1110 return InternalFlagsFormat((RuntimeType)GetType(), ToUInt64()) ?? ValueToString();
1111 }
1112 }
1114 }
1115
1116 [Obsolete("The provider argument is not used. Use ToString() instead.")]
1117 public string ToString(IFormatProvider? provider)
1118 {
1119 return ToString();
1120 }
1121
1123 {
1124 return InternalGetCorElementType() switch
1125 {
1126 CorElementType.ELEMENT_TYPE_I1 => TypeCode.SByte,
1127 CorElementType.ELEMENT_TYPE_U1 => TypeCode.Byte,
1128 CorElementType.ELEMENT_TYPE_BOOLEAN => TypeCode.Boolean,
1129 CorElementType.ELEMENT_TYPE_I2 => TypeCode.Int16,
1130 CorElementType.ELEMENT_TYPE_U2 => TypeCode.UInt16,
1131 CorElementType.ELEMENT_TYPE_CHAR => TypeCode.Char,
1132 CorElementType.ELEMENT_TYPE_I4 => TypeCode.Int32,
1133 CorElementType.ELEMENT_TYPE_U4 => TypeCode.UInt32,
1134 CorElementType.ELEMENT_TYPE_I8 => TypeCode.Int64,
1135 CorElementType.ELEMENT_TYPE_U8 => TypeCode.UInt64,
1137 };
1138 }
1139
1141 {
1142 return Convert.ToBoolean(GetValue());
1143 }
1144
1146 {
1147 return Convert.ToChar(GetValue());
1148 }
1149
1151 {
1152 return Convert.ToSByte(GetValue());
1153 }
1154
1156 {
1157 return Convert.ToByte(GetValue());
1158 }
1159
1161 {
1162 return Convert.ToInt16(GetValue());
1163 }
1164
1166 {
1167 return Convert.ToUInt16(GetValue());
1168 }
1169
1171 {
1172 return Convert.ToInt32(GetValue());
1173 }
1174
1176 {
1177 return Convert.ToUInt32(GetValue());
1178 }
1179
1181 {
1182 return Convert.ToInt64(GetValue());
1183 }
1184
1186 {
1187 return Convert.ToUInt64(GetValue());
1188 }
1189
1191 {
1192 return Convert.ToSingle(GetValue());
1193 }
1194
1196 {
1197 return Convert.ToDouble(GetValue());
1198 }
1199
1201 {
1202 return Convert.ToDecimal(GetValue());
1203 }
1204
1206 {
1207 throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, "Enum", "DateTime"));
1208 }
1209
1211 {
1212 return Convert.DefaultToType(this, type, provider);
1213 }
1214
1215 [CLSCompliant(false)]
1216 public static object ToObject(Type enumType, sbyte value)
1217 {
1218 return InternalBoxEnum(ValidateRuntimeType(enumType), value);
1219 }
1220
1221 public static object ToObject(Type enumType, short value)
1222 {
1223 return InternalBoxEnum(ValidateRuntimeType(enumType), value);
1224 }
1225
1226 public static object ToObject(Type enumType, int value)
1227 {
1228 return InternalBoxEnum(ValidateRuntimeType(enumType), value);
1229 }
1230
1231 public static object ToObject(Type enumType, byte value)
1232 {
1233 return InternalBoxEnum(ValidateRuntimeType(enumType), value);
1234 }
1235
1236 [CLSCompliant(false)]
1237 public static object ToObject(Type enumType, ushort value)
1238 {
1239 return InternalBoxEnum(ValidateRuntimeType(enumType), value);
1240 }
1241
1242 [CLSCompliant(false)]
1243 public static object ToObject(Type enumType, uint value)
1244 {
1245 return InternalBoxEnum(ValidateRuntimeType(enumType), value);
1246 }
1247
1248 public static object ToObject(Type enumType, long value)
1249 {
1250 return InternalBoxEnum(ValidateRuntimeType(enumType), value);
1251 }
1252
1253 [CLSCompliant(false)]
1254 public static object ToObject(Type enumType, ulong value)
1255 {
1256 return InternalBoxEnum(ValidateRuntimeType(enumType), (long)value);
1257 }
1258
1259 private static object ToObject(Type enumType, char value)
1260 {
1261 return InternalBoxEnum(ValidateRuntimeType(enumType), value);
1262 }
1263
1264 private static object ToObject(Type enumType, bool value)
1265 {
1266 return InternalBoxEnum(ValidateRuntimeType(enumType), value ? 1 : 0);
1267 }
1268
1269 private static RuntimeType ValidateRuntimeType(Type enumType)
1270 {
1271 if (enumType == null)
1272 {
1273 throw new ArgumentNullException("enumType");
1274 }
1275 if (!enumType.IsEnum)
1276 {
1277 throw new ArgumentException(SR.Arg_MustBeEnum, "enumType");
1278 }
1279 if (!(enumType is RuntimeType result))
1280 {
1281 throw new ArgumentException(SR.Arg_MustBeType, "enumType");
1282 }
1283 return result;
1284 }
1285}
static int BinarySearch(Array array, object? value)
Definition Array.cs:1320
static void WriteUInt32BigEndian(Span< byte > destination, uint value)
static void WriteUInt16BigEndian(Span< byte > destination, ushort value)
static void WriteUInt64BigEndian(Span< byte > destination, ulong value)
static decimal ToDecimal(object? value)
Definition Convert.cs:2101
static long ToInt64(object? value)
Definition Convert.cs:1623
static float ToSingle(object? value)
Definition Convert.cs:1881
static int ToInt32(object? value)
Definition Convert.cs:1320
static short ToInt16(object? value)
Definition Convert.cs:1038
static byte ToByte(object? value)
Definition Convert.cs:900
static uint ToUInt32(object? value)
Definition Convert.cs:1470
static ulong ToUInt64(object? value)
Definition Convert.cs:1738
static char ToChar(object? value)
Definition Convert.cs:618
static ushort ToUInt16(object? value)
Definition Convert.cs:1177
static ? object ChangeType(object? value, TypeCode typeCode)
Definition Convert.cs:229
static sbyte ToSByte(object? value)
Definition Convert.cs:745
static double ToDouble(object? value)
Definition Convert.cs:1991
static bool ToBoolean([NotNullWhen(true)] object? value)
Definition Convert.cs:508
static TypeCode GetTypeCode(object? value)
Definition Convert.cs:202
static object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
Definition Convert.cs:269
readonly bool HasFlagsAttribute
Definition Enum.cs:17
EnumInfo(bool hasFlagsAttribute, ulong[] values, string[] names)
Definition Enum.cs:23
readonly ulong[] Values
Definition Enum.cs:19
readonly string[] Names
Definition Enum.cs:21
static Type GetUnderlyingType(Type enumType)
Definition Enum.cs:309
static bool IsDefined< TEnum >(TEnum value)
Definition Enum.cs:351
int CompareTo(object? target)
Definition Enum.cs:1036
static string[] GetNames(Type enumType)
Definition Enum.cs:295
const char EnumSeparatorChar
Definition Enum.cs:31
static bool IsDefined(Type enumType, object value)
Definition Enum.cs:359
static bool TryParseUInt64Enum(RuntimeType enumType, ReadOnlySpan< char > value, bool ignoreCase, bool throwOnFailure, out ulong result)
Definition Enum.cs:733
static object Parse(Type enumType, string value)
Definition Enum.cs:368
static string InternalFlagsFormat(RuntimeType enumType, ulong result)
Definition Enum.cs:172
static ulong ToUInt64< TEnum >(TEnum value)
Definition Enum.cs:258
static object ToObject(Type enumType, char value)
Definition Enum.cs:1259
static bool TryParse(Type enumType, string? value, bool ignoreCase, out object? result)
Definition Enum.cs:426
string ToString(string? format, IFormatProvider? provider)
Definition Enum.cs:1084
static Array GetValues(Type enumType)
Definition Enum.cs:323
static object ToObject(Type enumType, uint value)
Definition Enum.cs:1243
static object Parse(Type enumType, ReadOnlySpan< char > value)
Definition Enum.cs:373
static bool TryParseRareEnum(RuntimeType enumType, ReadOnlySpan< char > value, bool ignoreCase, bool throwOnFailure, [NotNullWhen(true)] out object result)
Definition Enum.cs:759
static object ToObject(Type enumType, object value)
Definition Enum.cs:874
static ulong[] InternalGetValues(RuntimeType enumType)
Definition Enum.cs:346
static bool TryParse(Type enumType, string value, bool ignoreCase, bool throwOnFailure, out object result)
Definition Enum.cs:436
static object ToObject(Type enumType, short value)
Definition Enum.cs:1221
static object ToObject(Type enumType, sbyte value)
Definition Enum.cs:1216
static bool TryParse< TEnum >([NotNullWhen(true)] string? value, out TEnum result)
Definition Enum.cs:522
static TEnum Parse< TEnum >(string value)
Definition Enum.cs:392
static TEnum[] GetValues< TEnum >()
Definition Enum.cs:318
override string ToString()
Definition Enum.cs:1031
static bool TryParseInt64Enum(RuntimeType enumType, ReadOnlySpan< char > value, bool ignoreCase, bool throwOnFailure, out long result)
Definition Enum.cs:705
static object ToObject(Type enumType, byte value)
Definition Enum.cs:1231
bool HasFlag(Enum flag)
Definition Enum.cs:333
static object InternalBoxEnum(RuntimeType enumType, long value)
static string[] InternalGetNames(RuntimeType enumType)
Definition Enum.cs:304
static bool TryParseInt32Enum(RuntimeType enumType, ReadOnlySpan< char > value, int minInclusive, int maxInclusive, bool ignoreCase, bool throwOnFailure, TypeCode type, out int result)
Definition Enum.cs:641
static bool StartsNumber(char c)
Definition Enum.cs:865
static RuntimeType ValidateRuntimeType(Type enumType)
Definition Enum.cs:1269
override int GetHashCode()
Definition Enum.cs:1008
static object Parse(Type enumType, ReadOnlySpan< char > value, bool ignoreCase)
Definition Enum.cs:385
static string GetEnumName(EnumInfo enumInfo, ulong ulValue)
Definition Enum.cs:152
static bool TryParse(Type enumType, string? value, out object? result)
Definition Enum.cs:416
static string Format(Type enumType, object value, string format)
Definition Enum.cs:896
static bool TryParse(Type enumType, ReadOnlySpan< char > value, bool ignoreCase, bool throwOnFailure, out object result)
Definition Enum.cs:450
string ValueToHexString()
Definition Enum.cs:89
static ? string GetName< TEnum >(TEnum value)
Definition Enum.cs:276
static string[] GetNames< TEnum >()
Definition Enum.cs:290
static string GetEnumName(RuntimeType enumType, ulong ulValue)
Definition Enum.cs:147
static bool TryParse(Type enumType, ReadOnlySpan< char > value, out object? result)
Definition Enum.cs:421
ulong ToUInt64()
Definition Enum.cs:969
static object ToObject(Type enumType, long value)
Definition Enum.cs:1248
object GetValue()
Definition Enum.cs:946
static string InternalFlagsFormat(EnumInfo enumInfo, ulong resultValue)
Definition Enum.cs:177
static object ToObject(Type enumType, int value)
Definition Enum.cs:1226
static object ToObject(Type enumType, ushort value)
Definition Enum.cs:1237
static string InternalFormat(RuntimeType enumType, ulong value)
Definition Enum.cs:162
static object ToObject(Type enumType, bool value)
Definition Enum.cs:1264
static bool TryParse(Type enumType, ReadOnlySpan< char > value, bool ignoreCase, out object? result)
Definition Enum.cs:431
static RuntimeType InternalGetUnderlyingType(RuntimeType enumType)
static ? string GetName(Type enumType, object value)
Definition Enum.cs:281
static void GetEnumValuesAndNames(QCallTypeHandle enumType, ObjectHandleOnStack values, ObjectHandleOnStack names, Interop.BOOL getNames)
TypeCode GetTypeCode()
Definition Enum.cs:1122
static bool TryParseUInt32Enum(RuntimeType enumType, ReadOnlySpan< char > value, uint maxInclusive, bool ignoreCase, bool throwOnFailure, TypeCode type, out uint result)
Definition Enum.cs:673
bool InternalHasFlag(Enum flags)
static object ToObject(Type enumType, ulong value)
Definition Enum.cs:1254
string ToString(IFormatProvider? provider)
Definition Enum.cs:1117
static ulong ToUInt64(object value)
Definition Enum.cs:240
static EnumInfo GetEnumInfo(RuntimeType enumType, bool getNames=true)
Definition Enum.cs:51
static string ValueToHexString(object value)
Definition Enum.cs:129
override bool Equals([NotNullWhen(true)] object? obj)
static bool TryParseByName(RuntimeType enumType, ReadOnlySpan< char > value, bool ignoreCase, bool throwOnFailure, out ulong result)
Definition Enum.cs:793
string ValueToString()
Definition Enum.cs:66
static object Parse(Type enumType, string value, bool ignoreCase)
Definition Enum.cs:378
string ToString(string? format)
Definition Enum.cs:1089
CorElementType InternalGetCorElementType()
static CultureInfo InvariantCulture
static unsafe string ToString(ReadOnlySpan< byte > bytes, Casing casing=Casing.Upper)
static void ThrowOverflowException(TypeCode type)
Definition Number.cs:5924
static ParsingStatus TryParseUInt32IntegerStyle(ReadOnlySpan< char > value, NumberStyles styles, NumberFormatInfo info, out uint result)
Definition Number.cs:4764
static ParsingStatus TryParseInt32IntegerStyle(ReadOnlySpan< char > value, NumberStyles styles, NumberFormatInfo info, out int result)
Definition Number.cs:4281
static ParsingStatus TryParseUInt64IntegerStyle(ReadOnlySpan< char > value, NumberStyles styles, NumberFormatInfo info, out ulong result)
Definition Number.cs:5148
static ParsingStatus TryParseInt64IntegerStyle(ReadOnlySpan< char > value, NumberStyles styles, NumberFormatInfo info, out long result)
Definition Number.cs:4492
override bool IsDefined(Type attributeType, bool inherit)
static string Arg_MustContainEnumInfo
Definition SR.cs:316
static string Arg_EnumValueNotFound
Definition SR.cs:150
static string Arg_MustBeEnum
Definition SR.cs:274
static string Arg_MustBeType
Definition SR.cs:302
static string InvalidCast_FromTo
Definition SR.cs:1392
static string Argument_EnumTypeDoesNotMatch
Definition SR.cs:598
static string Arg_EnumAndObjectMustBeSameType
Definition SR.cs:140
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Arg_EnumFormatUnderlyingTypeAndObjectMustBeSameType
Definition SR.cs:142
static string InvalidOperation_UnknownEnumType
Definition SR.cs:1530
static string Format_InvalidEnumFormatSpecification
Definition SR.cs:1350
static string Arg_MustBeEnumBaseTypeOrEnum
Definition SR.cs:276
Definition SR.cs:7
static ? Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Definition Type.cs:408
virtual ? string GetEnumName(object value)
Definition Type.cs:1189
virtual RuntimeTypeHandle GetTypeHandleInternal()
Definition Type.cs:449
virtual string[] GetEnumNames()
Definition Type.cs:1214
virtual bool IsEnum
Definition Type.cs:227
virtual bool IsEnumDefined(object value)
Definition Type.cs:1147
static TypeCode GetTypeCode(Type? type)
Definition Type.cs:919
virtual Type GetEnumUnderlyingType()
Definition Type.cs:1035
virtual Array GetEnumValues()
Definition Type.cs:1049
short ToInt16(IFormatProvider? provider)
char ToChar(IFormatProvider? provider)
byte ToByte(IFormatProvider? provider)
decimal ToDecimal(IFormatProvider? provider)
object ToType(Type conversionType, IFormatProvider? provider)
uint ToUInt32(IFormatProvider? provider)
DateTime ToDateTime(IFormatProvider? provider)
int ToInt32(IFormatProvider? provider)
long ToInt64(IFormatProvider? provider)
ushort ToUInt16(IFormatProvider? provider)
double ToDouble(IFormatProvider? provider)
float ToSingle(IFormatProvider? provider)
sbyte ToSByte(IFormatProvider? provider)
ulong ToUInt64(IFormatProvider? provider)
bool ToBoolean(IFormatProvider? provider)
TypeCode
Definition TypeCode.cs:4
ReadOnlySpan< T > Slice(int start)
override string ToString()