ThrowHelper.cs 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. // This file defines an internal class used to throw exceptions in BCL code.
  5. // The main purpose is to reduce code size.
  6. //
  7. // The old way to throw an exception generates quite a lot IL code and assembly code.
  8. // Following is an example:
  9. // C# source
  10. // throw new ArgumentNullException(nameof(key), SR.ArgumentNull_Key);
  11. // IL code:
  12. // IL_0003: ldstr "key"
  13. // IL_0008: ldstr "ArgumentNull_Key"
  14. // IL_000d: call string System.Environment::GetResourceString(string)
  15. // IL_0012: newobj instance void System.ArgumentNullException::.ctor(string,string)
  16. // IL_0017: throw
  17. // which is 21bytes in IL.
  18. //
  19. // So we want to get rid of the ldstr and call to Environment.GetResource in IL.
  20. // In order to do that, I created two enums: ExceptionResource, ExceptionArgument to represent the
  21. // argument name and resource name in a small integer. The source code will be changed to
  22. // ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key, ExceptionResource.ArgumentNull_Key);
  23. //
  24. // The IL code will be 7 bytes.
  25. // IL_0008: ldc.i4.4
  26. // IL_0009: ldc.i4.4
  27. // IL_000a: call void System.ThrowHelper::ThrowArgumentNullException(valuetype System.ExceptionArgument)
  28. // IL_000f: ldarg.0
  29. //
  30. // This will also reduce the Jitted code size a lot.
  31. //
  32. // It is very important we do this for generic classes because we can easily generate the same code
  33. // multiple times for different instantiation.
  34. //
  35. using System.Buffers;
  36. using System.Collections.Generic;
  37. using System.Diagnostics;
  38. using System.Diagnostics.CodeAnalysis;
  39. using System.Runtime.CompilerServices;
  40. using System.Runtime.Serialization;
  41. namespace System
  42. {
  43. [StackTraceHidden]
  44. internal static class ThrowHelper
  45. {
  46. [DoesNotReturn]
  47. internal static void ThrowArrayTypeMismatchException()
  48. {
  49. throw new ArrayTypeMismatchException();
  50. }
  51. [DoesNotReturn]
  52. internal static void ThrowInvalidTypeWithPointersNotSupported(Type targetType)
  53. {
  54. throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, targetType));
  55. }
  56. [DoesNotReturn]
  57. internal static void ThrowIndexOutOfRangeException()
  58. {
  59. throw new IndexOutOfRangeException();
  60. }
  61. [DoesNotReturn]
  62. internal static void ThrowArgumentOutOfRangeException()
  63. {
  64. throw new ArgumentOutOfRangeException();
  65. }
  66. [DoesNotReturn]
  67. internal static void ThrowArgumentException_DestinationTooShort()
  68. {
  69. throw new ArgumentException(SR.Argument_DestinationTooShort, "destination");
  70. }
  71. [DoesNotReturn]
  72. internal static void ThrowArgumentException_OverlapAlignmentMismatch()
  73. {
  74. throw new ArgumentException(SR.Argument_OverlapAlignmentMismatch);
  75. }
  76. [DoesNotReturn]
  77. internal static void ThrowArgumentException_CannotExtractScalar(ExceptionArgument argument)
  78. {
  79. throw GetArgumentException(ExceptionResource.Argument_CannotExtractScalar, argument);
  80. }
  81. [DoesNotReturn]
  82. internal static void ThrowArgumentOutOfRange_IndexException()
  83. {
  84. throw GetArgumentOutOfRangeException(ExceptionArgument.index,
  85. ExceptionResource.ArgumentOutOfRange_Index);
  86. }
  87. [DoesNotReturn]
  88. internal static void ThrowIndexArgumentOutOfRange_NeedNonNegNumException()
  89. {
  90. throw GetArgumentOutOfRangeException(ExceptionArgument.index,
  91. ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
  92. }
  93. [DoesNotReturn]
  94. internal static void ThrowValueArgumentOutOfRange_NeedNonNegNumException()
  95. {
  96. throw GetArgumentOutOfRangeException(ExceptionArgument.value,
  97. ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
  98. }
  99. [DoesNotReturn]
  100. internal static void ThrowLengthArgumentOutOfRange_ArgumentOutOfRange_NeedNonNegNum()
  101. {
  102. throw GetArgumentOutOfRangeException(ExceptionArgument.length,
  103. ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
  104. }
  105. [DoesNotReturn]
  106. internal static void ThrowStartIndexArgumentOutOfRange_ArgumentOutOfRange_Index()
  107. {
  108. throw GetArgumentOutOfRangeException(ExceptionArgument.startIndex,
  109. ExceptionResource.ArgumentOutOfRange_Index);
  110. }
  111. [DoesNotReturn]
  112. internal static void ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count()
  113. {
  114. throw GetArgumentOutOfRangeException(ExceptionArgument.count,
  115. ExceptionResource.ArgumentOutOfRange_Count);
  116. }
  117. [DoesNotReturn]
  118. internal static void ThrowArgumentOutOfRange_Year()
  119. {
  120. throw GetArgumentOutOfRangeException(ExceptionArgument.year,
  121. ExceptionResource.ArgumentOutOfRange_Year);
  122. }
  123. [DoesNotReturn]
  124. internal static void ThrowArgumentOutOfRange_BadYearMonthDay()
  125. {
  126. throw new ArgumentOutOfRangeException(null, SR.ArgumentOutOfRange_BadYearMonthDay);
  127. }
  128. [DoesNotReturn]
  129. internal static void ThrowArgumentOutOfRange_BadHourMinuteSecond()
  130. {
  131. throw new ArgumentOutOfRangeException(null, SR.ArgumentOutOfRange_BadHourMinuteSecond);
  132. }
  133. [DoesNotReturn]
  134. internal static void ThrowArgumentOutOfRange_TimeSpanTooLong()
  135. {
  136. throw new ArgumentOutOfRangeException(null, SR.Overflow_TimeSpanTooLong);
  137. }
  138. [DoesNotReturn]
  139. internal static void ThrowWrongKeyTypeArgumentException<T>(T key, Type targetType)
  140. {
  141. // Generic key to move the boxing to the right hand side of throw
  142. throw GetWrongKeyTypeArgumentException((object?)key, targetType);
  143. }
  144. [DoesNotReturn]
  145. internal static void ThrowWrongValueTypeArgumentException<T>(T value, Type targetType)
  146. {
  147. // Generic key to move the boxing to the right hand side of throw
  148. throw GetWrongValueTypeArgumentException((object?)value, targetType);
  149. }
  150. private static ArgumentException GetAddingDuplicateWithKeyArgumentException(object? key)
  151. {
  152. return new ArgumentException(SR.Format(SR.Argument_AddingDuplicateWithKey, key));
  153. }
  154. [DoesNotReturn]
  155. internal static void ThrowAddingDuplicateWithKeyArgumentException<T>(T key)
  156. {
  157. // Generic key to move the boxing to the right hand side of throw
  158. throw GetAddingDuplicateWithKeyArgumentException((object?)key);
  159. }
  160. [DoesNotReturn]
  161. internal static void ThrowKeyNotFoundException<T>(T key)
  162. {
  163. // Generic key to move the boxing to the right hand side of throw
  164. throw GetKeyNotFoundException((object?)key);
  165. }
  166. [DoesNotReturn]
  167. internal static void ThrowArgumentException(ExceptionResource resource)
  168. {
  169. throw GetArgumentException(resource);
  170. }
  171. [DoesNotReturn]
  172. internal static void ThrowArgumentException(ExceptionResource resource, ExceptionArgument argument)
  173. {
  174. throw GetArgumentException(resource, argument);
  175. }
  176. private static ArgumentNullException GetArgumentNullException(ExceptionArgument argument)
  177. {
  178. return new ArgumentNullException(GetArgumentName(argument));
  179. }
  180. [DoesNotReturn]
  181. internal static void ThrowArgumentNullException(ExceptionArgument argument)
  182. {
  183. throw GetArgumentNullException(argument);
  184. }
  185. [DoesNotReturn]
  186. internal static void ThrowArgumentNullException(ExceptionResource resource)
  187. {
  188. throw new ArgumentNullException(GetResourceString(resource));
  189. }
  190. [DoesNotReturn]
  191. internal static void ThrowArgumentNullException(ExceptionArgument argument, ExceptionResource resource)
  192. {
  193. throw new ArgumentNullException(GetArgumentName(argument), GetResourceString(resource));
  194. }
  195. [DoesNotReturn]
  196. internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument)
  197. {
  198. throw new ArgumentOutOfRangeException(GetArgumentName(argument));
  199. }
  200. [DoesNotReturn]
  201. internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
  202. {
  203. throw GetArgumentOutOfRangeException(argument, resource);
  204. }
  205. [DoesNotReturn]
  206. internal static void ThrowArgumentOutOfRangeException(ExceptionArgument argument, int paramNumber, ExceptionResource resource)
  207. {
  208. throw GetArgumentOutOfRangeException(argument, paramNumber, resource);
  209. }
  210. [DoesNotReturn]
  211. internal static void ThrowInvalidOperationException()
  212. {
  213. throw new InvalidOperationException();
  214. }
  215. [DoesNotReturn]
  216. internal static void ThrowInvalidOperationException(ExceptionResource resource)
  217. {
  218. throw GetInvalidOperationException(resource);
  219. }
  220. [DoesNotReturn]
  221. internal static void ThrowInvalidOperationException_OutstandingReferences()
  222. {
  223. throw new InvalidOperationException(SR.Memory_OutstandingReferences);
  224. }
  225. [DoesNotReturn]
  226. internal static void ThrowInvalidOperationException(ExceptionResource resource, Exception e)
  227. {
  228. throw new InvalidOperationException(GetResourceString(resource), e);
  229. }
  230. [DoesNotReturn]
  231. internal static void ThrowSerializationException(ExceptionResource resource)
  232. {
  233. throw new SerializationException(GetResourceString(resource));
  234. }
  235. [DoesNotReturn]
  236. internal static void ThrowSecurityException(ExceptionResource resource)
  237. {
  238. throw new System.Security.SecurityException(GetResourceString(resource));
  239. }
  240. [DoesNotReturn]
  241. internal static void ThrowRankException(ExceptionResource resource)
  242. {
  243. throw new RankException(GetResourceString(resource));
  244. }
  245. [DoesNotReturn]
  246. internal static void ThrowNotSupportedException(ExceptionResource resource)
  247. {
  248. throw new NotSupportedException(GetResourceString(resource));
  249. }
  250. [DoesNotReturn]
  251. internal static void ThrowUnauthorizedAccessException(ExceptionResource resource)
  252. {
  253. throw new UnauthorizedAccessException(GetResourceString(resource));
  254. }
  255. [DoesNotReturn]
  256. internal static void ThrowObjectDisposedException(string objectName, ExceptionResource resource)
  257. {
  258. throw new ObjectDisposedException(objectName, GetResourceString(resource));
  259. }
  260. [DoesNotReturn]
  261. internal static void ThrowObjectDisposedException(ExceptionResource resource)
  262. {
  263. throw new ObjectDisposedException(null, GetResourceString(resource));
  264. }
  265. [DoesNotReturn]
  266. internal static void ThrowNotSupportedException()
  267. {
  268. throw new NotSupportedException();
  269. }
  270. [DoesNotReturn]
  271. internal static void ThrowAggregateException(List<Exception> exceptions)
  272. {
  273. throw new AggregateException(exceptions);
  274. }
  275. [DoesNotReturn]
  276. internal static void ThrowOutOfMemoryException()
  277. {
  278. throw new OutOfMemoryException();
  279. }
  280. [DoesNotReturn]
  281. internal static void ThrowArgumentException_Argument_InvalidArrayType()
  282. {
  283. throw new ArgumentException(SR.Argument_InvalidArrayType);
  284. }
  285. [DoesNotReturn]
  286. internal static void ThrowInvalidOperationException_InvalidOperation_EnumNotStarted()
  287. {
  288. throw new InvalidOperationException(SR.InvalidOperation_EnumNotStarted);
  289. }
  290. [DoesNotReturn]
  291. internal static void ThrowInvalidOperationException_InvalidOperation_EnumEnded()
  292. {
  293. throw new InvalidOperationException(SR.InvalidOperation_EnumEnded);
  294. }
  295. [DoesNotReturn]
  296. internal static void ThrowInvalidOperationException_EnumCurrent(int index)
  297. {
  298. throw GetInvalidOperationException_EnumCurrent(index);
  299. }
  300. [DoesNotReturn]
  301. internal static void ThrowInvalidOperationException_InvalidOperation_EnumFailedVersion()
  302. {
  303. throw new InvalidOperationException(SR.InvalidOperation_EnumFailedVersion);
  304. }
  305. [DoesNotReturn]
  306. internal static void ThrowInvalidOperationException_InvalidOperation_EnumOpCantHappen()
  307. {
  308. throw new InvalidOperationException(SR.InvalidOperation_EnumOpCantHappen);
  309. }
  310. [DoesNotReturn]
  311. internal static void ThrowInvalidOperationException_InvalidOperation_NoValue()
  312. {
  313. throw new InvalidOperationException(SR.InvalidOperation_NoValue);
  314. }
  315. [DoesNotReturn]
  316. internal static void ThrowInvalidOperationException_ConcurrentOperationsNotSupported()
  317. {
  318. throw new InvalidOperationException(SR.InvalidOperation_ConcurrentOperationsNotSupported);
  319. }
  320. [DoesNotReturn]
  321. internal static void ThrowInvalidOperationException_HandleIsNotInitialized()
  322. {
  323. throw new InvalidOperationException(SR.InvalidOperation_HandleIsNotInitialized);
  324. }
  325. [DoesNotReturn]
  326. internal static void ThrowInvalidOperationException_HandleIsNotPinned()
  327. {
  328. throw new InvalidOperationException(SR.InvalidOperation_HandleIsNotPinned);
  329. }
  330. [DoesNotReturn]
  331. internal static void ThrowArraySegmentCtorValidationFailedExceptions(Array? array, int offset, int count)
  332. {
  333. throw GetArraySegmentCtorValidationFailedException(array, offset, count);
  334. }
  335. [DoesNotReturn]
  336. internal static void ThrowFormatException_BadFormatSpecifier()
  337. {
  338. throw new FormatException(SR.Argument_BadFormatSpecifier);
  339. }
  340. [DoesNotReturn]
  341. internal static void ThrowArgumentOutOfRangeException_PrecisionTooLarge()
  342. {
  343. throw new ArgumentOutOfRangeException("precision", SR.Format(SR.Argument_PrecisionTooLarge, StandardFormat.MaxPrecision));
  344. }
  345. [DoesNotReturn]
  346. internal static void ThrowArgumentOutOfRangeException_SymbolDoesNotFit()
  347. {
  348. throw new ArgumentOutOfRangeException("symbol", SR.Argument_BadFormatSpecifier);
  349. }
  350. private static Exception GetArraySegmentCtorValidationFailedException(Array? array, int offset, int count)
  351. {
  352. if (array == null)
  353. return new ArgumentNullException(nameof(array));
  354. if (offset < 0)
  355. return new ArgumentOutOfRangeException(nameof(offset), SR.ArgumentOutOfRange_NeedNonNegNum);
  356. if (count < 0)
  357. return new ArgumentOutOfRangeException(nameof(count), SR.ArgumentOutOfRange_NeedNonNegNum);
  358. Debug.Assert(array.Length - offset < count);
  359. return new ArgumentException(SR.Argument_InvalidOffLen);
  360. }
  361. private static ArgumentException GetArgumentException(ExceptionResource resource)
  362. {
  363. return new ArgumentException(GetResourceString(resource));
  364. }
  365. private static InvalidOperationException GetInvalidOperationException(ExceptionResource resource)
  366. {
  367. return new InvalidOperationException(GetResourceString(resource));
  368. }
  369. private static ArgumentException GetWrongKeyTypeArgumentException(object? key, Type targetType)
  370. {
  371. return new ArgumentException(SR.Format(SR.Arg_WrongType, key, targetType), nameof(key));
  372. }
  373. private static ArgumentException GetWrongValueTypeArgumentException(object? value, Type targetType)
  374. {
  375. return new ArgumentException(SR.Format(SR.Arg_WrongType, value, targetType), nameof(value));
  376. }
  377. private static KeyNotFoundException GetKeyNotFoundException(object? key)
  378. {
  379. return new KeyNotFoundException(SR.Format(SR.Arg_KeyNotFoundWithKey, key));
  380. }
  381. private static ArgumentOutOfRangeException GetArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
  382. {
  383. return new ArgumentOutOfRangeException(GetArgumentName(argument), GetResourceString(resource));
  384. }
  385. private static ArgumentException GetArgumentException(ExceptionResource resource, ExceptionArgument argument)
  386. {
  387. return new ArgumentException(GetResourceString(resource), GetArgumentName(argument));
  388. }
  389. private static ArgumentOutOfRangeException GetArgumentOutOfRangeException(ExceptionArgument argument, int paramNumber, ExceptionResource resource)
  390. {
  391. return new ArgumentOutOfRangeException(GetArgumentName(argument) + "[" + paramNumber.ToString() + "]", GetResourceString(resource));
  392. }
  393. private static InvalidOperationException GetInvalidOperationException_EnumCurrent(int index)
  394. {
  395. return new InvalidOperationException(
  396. index < 0 ?
  397. SR.InvalidOperation_EnumNotStarted :
  398. SR.InvalidOperation_EnumEnded);
  399. }
  400. // Allow nulls for reference types and Nullable<U>, but not for value types.
  401. // Aggressively inline so the jit evaluates the if in place and either drops the call altogether
  402. // Or just leaves null test and call to the Non-returning ThrowHelper.ThrowArgumentNullException
  403. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  404. internal static void IfNullAndNullsAreIllegalThenThrow<T>(object? value, ExceptionArgument argName)
  405. {
  406. // Note that default(T) is not equal to null for value types except when T is Nullable<U>.
  407. if (!(default(T)! == null) && value == null) // TODO-NULLABLE: default(T) == null warning (https://github.com/dotnet/roslyn/issues/34757)
  408. ThrowHelper.ThrowArgumentNullException(argName);
  409. }
  410. // Throws if 'T' is disallowed in Vector<T> / Vector128<T> / other related types in the
  411. // Numerics or Intrinsics namespaces. If 'T' is allowed, no-ops. JIT will elide the method
  412. // entirely if 'T' is supported and we're on an optimized release build.
  413. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  414. internal static void ThrowForUnsupportedVectorBaseType<T>() where T : struct
  415. {
  416. if (typeof(T) != typeof(byte) && typeof(T) != typeof(sbyte) &&
  417. typeof(T) != typeof(short) && typeof(T) != typeof(ushort) &&
  418. typeof(T) != typeof(int) && typeof(T) != typeof(uint) &&
  419. typeof(T) != typeof(long) && typeof(T) != typeof(ulong) &&
  420. typeof(T) != typeof(float) && typeof(T) != typeof(double))
  421. {
  422. ThrowNotSupportedException(ExceptionResource.Arg_TypeNotSupported);
  423. }
  424. }
  425. #if false // Reflection-based implementation does not work for CoreRT/ProjectN
  426. // This function will convert an ExceptionArgument enum value to the argument name string.
  427. [MethodImpl(MethodImplOptions.NoInlining)]
  428. private static string GetArgumentName(ExceptionArgument argument)
  429. {
  430. Debug.Assert(Enum.IsDefined(typeof(ExceptionArgument), argument),
  431. "The enum value is not defined, please check the ExceptionArgument Enum.");
  432. return argument.ToString();
  433. }
  434. #endif
  435. private static string GetArgumentName(ExceptionArgument argument)
  436. {
  437. switch (argument)
  438. {
  439. case ExceptionArgument.obj:
  440. return "obj";
  441. case ExceptionArgument.dictionary:
  442. return "dictionary";
  443. case ExceptionArgument.array:
  444. return "array";
  445. case ExceptionArgument.info:
  446. return "info";
  447. case ExceptionArgument.key:
  448. return "key";
  449. case ExceptionArgument.text:
  450. return "text";
  451. case ExceptionArgument.values:
  452. return "values";
  453. case ExceptionArgument.value:
  454. return "value";
  455. case ExceptionArgument.startIndex:
  456. return "startIndex";
  457. case ExceptionArgument.task:
  458. return "task";
  459. case ExceptionArgument.bytes:
  460. return "bytes";
  461. case ExceptionArgument.byteIndex:
  462. return "byteIndex";
  463. case ExceptionArgument.byteCount:
  464. return "byteCount";
  465. case ExceptionArgument.ch:
  466. return "ch";
  467. case ExceptionArgument.chars:
  468. return "chars";
  469. case ExceptionArgument.charIndex:
  470. return "charIndex";
  471. case ExceptionArgument.charCount:
  472. return "charCount";
  473. case ExceptionArgument.s:
  474. return "s";
  475. case ExceptionArgument.input:
  476. return "input";
  477. case ExceptionArgument.ownedMemory:
  478. return "ownedMemory";
  479. case ExceptionArgument.list:
  480. return "list";
  481. case ExceptionArgument.index:
  482. return "index";
  483. case ExceptionArgument.capacity:
  484. return "capacity";
  485. case ExceptionArgument.collection:
  486. return "collection";
  487. case ExceptionArgument.item:
  488. return "item";
  489. case ExceptionArgument.converter:
  490. return "converter";
  491. case ExceptionArgument.match:
  492. return "match";
  493. case ExceptionArgument.count:
  494. return "count";
  495. case ExceptionArgument.action:
  496. return "action";
  497. case ExceptionArgument.comparison:
  498. return "comparison";
  499. case ExceptionArgument.exceptions:
  500. return "exceptions";
  501. case ExceptionArgument.exception:
  502. return "exception";
  503. case ExceptionArgument.pointer:
  504. return "pointer";
  505. case ExceptionArgument.start:
  506. return "start";
  507. case ExceptionArgument.format:
  508. return "format";
  509. case ExceptionArgument.culture:
  510. return "culture";
  511. case ExceptionArgument.comparer:
  512. return "comparer";
  513. case ExceptionArgument.comparable:
  514. return "comparable";
  515. case ExceptionArgument.source:
  516. return "source";
  517. case ExceptionArgument.state:
  518. return "state";
  519. case ExceptionArgument.length:
  520. return "length";
  521. case ExceptionArgument.comparisonType:
  522. return "comparisonType";
  523. case ExceptionArgument.manager:
  524. return "manager";
  525. case ExceptionArgument.sourceBytesToCopy:
  526. return "sourceBytesToCopy";
  527. case ExceptionArgument.callBack:
  528. return "callBack";
  529. case ExceptionArgument.creationOptions:
  530. return "creationOptions";
  531. case ExceptionArgument.function:
  532. return "function";
  533. case ExceptionArgument.scheduler:
  534. return "scheduler";
  535. case ExceptionArgument.continuationAction:
  536. return "continuationAction";
  537. case ExceptionArgument.continuationFunction:
  538. return "continuationFunction";
  539. case ExceptionArgument.tasks:
  540. return "tasks";
  541. case ExceptionArgument.asyncResult:
  542. return "asyncResult";
  543. case ExceptionArgument.beginMethod:
  544. return "beginMethod";
  545. case ExceptionArgument.endMethod:
  546. return "endMethod";
  547. case ExceptionArgument.endFunction:
  548. return "endFunction";
  549. case ExceptionArgument.cancellationToken:
  550. return "cancellationToken";
  551. case ExceptionArgument.continuationOptions:
  552. return "continuationOptions";
  553. case ExceptionArgument.delay:
  554. return "delay";
  555. case ExceptionArgument.millisecondsDelay:
  556. return "millisecondsDelay";
  557. case ExceptionArgument.millisecondsTimeout:
  558. return "millisecondsTimeout";
  559. case ExceptionArgument.stateMachine:
  560. return "stateMachine";
  561. case ExceptionArgument.timeout:
  562. return "timeout";
  563. case ExceptionArgument.type:
  564. return "type";
  565. case ExceptionArgument.sourceIndex:
  566. return "sourceIndex";
  567. case ExceptionArgument.sourceArray:
  568. return "sourceArray";
  569. case ExceptionArgument.destinationIndex:
  570. return "destinationIndex";
  571. case ExceptionArgument.destinationArray:
  572. return "destinationArray";
  573. case ExceptionArgument.pHandle:
  574. return "pHandle";
  575. case ExceptionArgument.other:
  576. return "other";
  577. case ExceptionArgument.newSize:
  578. return "newSize";
  579. case ExceptionArgument.lowerBounds:
  580. return "lowerBounds";
  581. case ExceptionArgument.lengths:
  582. return "lengths";
  583. case ExceptionArgument.len:
  584. return "len";
  585. case ExceptionArgument.keys:
  586. return "keys";
  587. case ExceptionArgument.indices:
  588. return "indices";
  589. case ExceptionArgument.index1:
  590. return "index1";
  591. case ExceptionArgument.index2:
  592. return "index2";
  593. case ExceptionArgument.index3:
  594. return "index3";
  595. case ExceptionArgument.length1:
  596. return "length1";
  597. case ExceptionArgument.length2:
  598. return "length2";
  599. case ExceptionArgument.length3:
  600. return "length3";
  601. case ExceptionArgument.endIndex:
  602. return "endIndex";
  603. case ExceptionArgument.elementType:
  604. return "elementType";
  605. case ExceptionArgument.arrayIndex:
  606. return "arrayIndex";
  607. case ExceptionArgument.year:
  608. return "year";
  609. default:
  610. Debug.Fail("The enum value is not defined, please check the ExceptionArgument Enum.");
  611. return "";
  612. }
  613. }
  614. #if false // Reflection-based implementation does not work for CoreRT/ProjectN
  615. // This function will convert an ExceptionResource enum value to the resource string.
  616. [MethodImpl(MethodImplOptions.NoInlining)]
  617. private static string GetResourceString(ExceptionResource resource)
  618. {
  619. Debug.Assert(Enum.IsDefined(typeof(ExceptionResource), resource),
  620. "The enum value is not defined, please check the ExceptionResource Enum.");
  621. return SR.GetResourceString(resource.ToString());
  622. }
  623. #endif
  624. private static string GetResourceString(ExceptionResource resource)
  625. {
  626. switch (resource)
  627. {
  628. case ExceptionResource.ArgumentOutOfRange_Index:
  629. return SR.ArgumentOutOfRange_Index;
  630. case ExceptionResource.ArgumentOutOfRange_IndexCount:
  631. return SR.ArgumentOutOfRange_IndexCount;
  632. case ExceptionResource.ArgumentOutOfRange_IndexCountBuffer:
  633. return SR.ArgumentOutOfRange_IndexCountBuffer;
  634. case ExceptionResource.ArgumentOutOfRange_Count:
  635. return SR.ArgumentOutOfRange_Count;
  636. case ExceptionResource.ArgumentOutOfRange_Year:
  637. return SR.ArgumentOutOfRange_Year;
  638. case ExceptionResource.Arg_ArrayPlusOffTooSmall:
  639. return SR.Arg_ArrayPlusOffTooSmall;
  640. case ExceptionResource.NotSupported_ReadOnlyCollection:
  641. return SR.NotSupported_ReadOnlyCollection;
  642. case ExceptionResource.Arg_RankMultiDimNotSupported:
  643. return SR.Arg_RankMultiDimNotSupported;
  644. case ExceptionResource.Arg_NonZeroLowerBound:
  645. return SR.Arg_NonZeroLowerBound;
  646. case ExceptionResource.ArgumentOutOfRange_ListInsert:
  647. return SR.ArgumentOutOfRange_ListInsert;
  648. case ExceptionResource.ArgumentOutOfRange_NeedNonNegNum:
  649. return SR.ArgumentOutOfRange_NeedNonNegNum;
  650. case ExceptionResource.ArgumentOutOfRange_SmallCapacity:
  651. return SR.ArgumentOutOfRange_SmallCapacity;
  652. case ExceptionResource.Argument_InvalidOffLen:
  653. return SR.Argument_InvalidOffLen;
  654. case ExceptionResource.Argument_CannotExtractScalar:
  655. return SR.Argument_CannotExtractScalar;
  656. case ExceptionResource.ArgumentOutOfRange_BiggerThanCollection:
  657. return SR.ArgumentOutOfRange_BiggerThanCollection;
  658. case ExceptionResource.Serialization_MissingKeys:
  659. return SR.Serialization_MissingKeys;
  660. case ExceptionResource.Serialization_NullKey:
  661. return SR.Serialization_NullKey;
  662. case ExceptionResource.NotSupported_KeyCollectionSet:
  663. return SR.NotSupported_KeyCollectionSet;
  664. case ExceptionResource.NotSupported_ValueCollectionSet:
  665. return SR.NotSupported_ValueCollectionSet;
  666. case ExceptionResource.InvalidOperation_NullArray:
  667. return SR.InvalidOperation_NullArray;
  668. case ExceptionResource.TaskT_TransitionToFinal_AlreadyCompleted:
  669. return SR.TaskT_TransitionToFinal_AlreadyCompleted;
  670. case ExceptionResource.TaskCompletionSourceT_TrySetException_NullException:
  671. return SR.TaskCompletionSourceT_TrySetException_NullException;
  672. case ExceptionResource.TaskCompletionSourceT_TrySetException_NoExceptions:
  673. return SR.TaskCompletionSourceT_TrySetException_NoExceptions;
  674. case ExceptionResource.NotSupported_StringComparison:
  675. return SR.NotSupported_StringComparison;
  676. case ExceptionResource.ConcurrentCollection_SyncRoot_NotSupported:
  677. return SR.ConcurrentCollection_SyncRoot_NotSupported;
  678. case ExceptionResource.Task_MultiTaskContinuation_NullTask:
  679. return SR.Task_MultiTaskContinuation_NullTask;
  680. case ExceptionResource.InvalidOperation_WrongAsyncResultOrEndCalledMultiple:
  681. return SR.InvalidOperation_WrongAsyncResultOrEndCalledMultiple;
  682. case ExceptionResource.Task_MultiTaskContinuation_EmptyTaskList:
  683. return SR.Task_MultiTaskContinuation_EmptyTaskList;
  684. case ExceptionResource.Task_Start_TaskCompleted:
  685. return SR.Task_Start_TaskCompleted;
  686. case ExceptionResource.Task_Start_Promise:
  687. return SR.Task_Start_Promise;
  688. case ExceptionResource.Task_Start_ContinuationTask:
  689. return SR.Task_Start_ContinuationTask;
  690. case ExceptionResource.Task_Start_AlreadyStarted:
  691. return SR.Task_Start_AlreadyStarted;
  692. case ExceptionResource.Task_RunSynchronously_Continuation:
  693. return SR.Task_RunSynchronously_Continuation;
  694. case ExceptionResource.Task_RunSynchronously_Promise:
  695. return SR.Task_RunSynchronously_Promise;
  696. case ExceptionResource.Task_RunSynchronously_TaskCompleted:
  697. return SR.Task_RunSynchronously_TaskCompleted;
  698. case ExceptionResource.Task_RunSynchronously_AlreadyStarted:
  699. return SR.Task_RunSynchronously_AlreadyStarted;
  700. case ExceptionResource.AsyncMethodBuilder_InstanceNotInitialized:
  701. return SR.AsyncMethodBuilder_InstanceNotInitialized;
  702. case ExceptionResource.Task_ContinueWith_ESandLR:
  703. return SR.Task_ContinueWith_ESandLR;
  704. case ExceptionResource.Task_ContinueWith_NotOnAnything:
  705. return SR.Task_ContinueWith_NotOnAnything;
  706. case ExceptionResource.Task_Delay_InvalidDelay:
  707. return SR.Task_Delay_InvalidDelay;
  708. case ExceptionResource.Task_Delay_InvalidMillisecondsDelay:
  709. return SR.Task_Delay_InvalidMillisecondsDelay;
  710. case ExceptionResource.Task_Dispose_NotCompleted:
  711. return SR.Task_Dispose_NotCompleted;
  712. case ExceptionResource.Task_ThrowIfDisposed:
  713. return SR.Task_ThrowIfDisposed;
  714. case ExceptionResource.Task_WaitMulti_NullTask:
  715. return SR.Task_WaitMulti_NullTask;
  716. case ExceptionResource.ArgumentException_OtherNotArrayOfCorrectLength:
  717. return SR.ArgumentException_OtherNotArrayOfCorrectLength;
  718. case ExceptionResource.ArgumentNull_Array:
  719. return SR.ArgumentNull_Array;
  720. case ExceptionResource.ArgumentNull_SafeHandle:
  721. return SR.ArgumentNull_SafeHandle;
  722. case ExceptionResource.ArgumentOutOfRange_EndIndexStartIndex:
  723. return SR.ArgumentOutOfRange_EndIndexStartIndex;
  724. case ExceptionResource.ArgumentOutOfRange_Enum:
  725. return SR.ArgumentOutOfRange_Enum;
  726. case ExceptionResource.ArgumentOutOfRange_HugeArrayNotSupported:
  727. return SR.ArgumentOutOfRange_HugeArrayNotSupported;
  728. case ExceptionResource.Argument_AddingDuplicate:
  729. return SR.Argument_AddingDuplicate;
  730. case ExceptionResource.Argument_InvalidArgumentForComparison:
  731. return SR.Argument_InvalidArgumentForComparison;
  732. case ExceptionResource.Arg_LowerBoundsMustMatch:
  733. return SR.Arg_LowerBoundsMustMatch;
  734. case ExceptionResource.Arg_MustBeType:
  735. return SR.Arg_MustBeType;
  736. case ExceptionResource.Arg_Need1DArray:
  737. return SR.Arg_Need1DArray;
  738. case ExceptionResource.Arg_Need2DArray:
  739. return SR.Arg_Need2DArray;
  740. case ExceptionResource.Arg_Need3DArray:
  741. return SR.Arg_Need3DArray;
  742. case ExceptionResource.Arg_NeedAtLeast1Rank:
  743. return SR.Arg_NeedAtLeast1Rank;
  744. case ExceptionResource.Arg_RankIndices:
  745. return SR.Arg_RankIndices;
  746. case ExceptionResource.Arg_RanksAndBounds:
  747. return SR.Arg_RanksAndBounds;
  748. case ExceptionResource.InvalidOperation_IComparerFailed:
  749. return SR.InvalidOperation_IComparerFailed;
  750. case ExceptionResource.NotSupported_FixedSizeCollection:
  751. return SR.NotSupported_FixedSizeCollection;
  752. case ExceptionResource.Rank_MultiDimNotSupported:
  753. return SR.Rank_MultiDimNotSupported;
  754. case ExceptionResource.Arg_TypeNotSupported:
  755. return SR.Arg_TypeNotSupported;
  756. case ExceptionResource.Argument_SpansMustHaveSameLength:
  757. return SR.Argument_SpansMustHaveSameLength;
  758. default:
  759. Debug.Fail("The enum value is not defined, please check the ExceptionResource Enum.");
  760. return "";
  761. }
  762. }
  763. }
  764. //
  765. // The convention for this enum is using the argument name as the enum name
  766. //
  767. internal enum ExceptionArgument
  768. {
  769. obj,
  770. dictionary,
  771. array,
  772. info,
  773. key,
  774. text,
  775. values,
  776. value,
  777. startIndex,
  778. task,
  779. bytes,
  780. byteIndex,
  781. byteCount,
  782. ch,
  783. chars,
  784. charIndex,
  785. charCount,
  786. s,
  787. input,
  788. ownedMemory,
  789. list,
  790. index,
  791. capacity,
  792. collection,
  793. item,
  794. converter,
  795. match,
  796. count,
  797. action,
  798. comparison,
  799. exceptions,
  800. exception,
  801. pointer,
  802. start,
  803. format,
  804. culture,
  805. comparer,
  806. comparable,
  807. source,
  808. state,
  809. length,
  810. comparisonType,
  811. manager,
  812. sourceBytesToCopy,
  813. callBack,
  814. creationOptions,
  815. function,
  816. scheduler,
  817. continuationAction,
  818. continuationFunction,
  819. tasks,
  820. asyncResult,
  821. beginMethod,
  822. endMethod,
  823. endFunction,
  824. cancellationToken,
  825. continuationOptions,
  826. delay,
  827. millisecondsDelay,
  828. millisecondsTimeout,
  829. stateMachine,
  830. timeout,
  831. type,
  832. sourceIndex,
  833. sourceArray,
  834. destinationIndex,
  835. destinationArray,
  836. pHandle,
  837. other,
  838. newSize,
  839. lowerBounds,
  840. lengths,
  841. len,
  842. keys,
  843. indices,
  844. index1,
  845. index2,
  846. index3,
  847. length1,
  848. length2,
  849. length3,
  850. endIndex,
  851. elementType,
  852. arrayIndex,
  853. year,
  854. }
  855. //
  856. // The convention for this enum is using the resource name as the enum name
  857. //
  858. internal enum ExceptionResource
  859. {
  860. ArgumentOutOfRange_Index,
  861. ArgumentOutOfRange_IndexCount,
  862. ArgumentOutOfRange_IndexCountBuffer,
  863. ArgumentOutOfRange_Count,
  864. ArgumentOutOfRange_Year,
  865. Arg_ArrayPlusOffTooSmall,
  866. NotSupported_ReadOnlyCollection,
  867. Arg_RankMultiDimNotSupported,
  868. Arg_NonZeroLowerBound,
  869. ArgumentOutOfRange_ListInsert,
  870. ArgumentOutOfRange_NeedNonNegNum,
  871. ArgumentOutOfRange_SmallCapacity,
  872. Argument_InvalidOffLen,
  873. Argument_CannotExtractScalar,
  874. ArgumentOutOfRange_BiggerThanCollection,
  875. Serialization_MissingKeys,
  876. Serialization_NullKey,
  877. NotSupported_KeyCollectionSet,
  878. NotSupported_ValueCollectionSet,
  879. InvalidOperation_NullArray,
  880. TaskT_TransitionToFinal_AlreadyCompleted,
  881. TaskCompletionSourceT_TrySetException_NullException,
  882. TaskCompletionSourceT_TrySetException_NoExceptions,
  883. NotSupported_StringComparison,
  884. ConcurrentCollection_SyncRoot_NotSupported,
  885. Task_MultiTaskContinuation_NullTask,
  886. InvalidOperation_WrongAsyncResultOrEndCalledMultiple,
  887. Task_MultiTaskContinuation_EmptyTaskList,
  888. Task_Start_TaskCompleted,
  889. Task_Start_Promise,
  890. Task_Start_ContinuationTask,
  891. Task_Start_AlreadyStarted,
  892. Task_RunSynchronously_Continuation,
  893. Task_RunSynchronously_Promise,
  894. Task_RunSynchronously_TaskCompleted,
  895. Task_RunSynchronously_AlreadyStarted,
  896. AsyncMethodBuilder_InstanceNotInitialized,
  897. Task_ContinueWith_ESandLR,
  898. Task_ContinueWith_NotOnAnything,
  899. Task_Delay_InvalidDelay,
  900. Task_Delay_InvalidMillisecondsDelay,
  901. Task_Dispose_NotCompleted,
  902. Task_ThrowIfDisposed,
  903. Task_WaitMulti_NullTask,
  904. ArgumentException_OtherNotArrayOfCorrectLength,
  905. ArgumentNull_Array,
  906. ArgumentNull_SafeHandle,
  907. ArgumentOutOfRange_EndIndexStartIndex,
  908. ArgumentOutOfRange_Enum,
  909. ArgumentOutOfRange_HugeArrayNotSupported,
  910. Argument_AddingDuplicate,
  911. Argument_InvalidArgumentForComparison,
  912. Arg_LowerBoundsMustMatch,
  913. Arg_MustBeType,
  914. Arg_Need1DArray,
  915. Arg_Need2DArray,
  916. Arg_Need3DArray,
  917. Arg_NeedAtLeast1Rank,
  918. Arg_RankIndices,
  919. Arg_RanksAndBounds,
  920. InvalidOperation_IComparerFailed,
  921. NotSupported_FixedSizeCollection,
  922. Rank_MultiDimNotSupported,
  923. Arg_TypeNotSupported,
  924. Argument_SpansMustHaveSameLength,
  925. }
  926. }