ThrowHelper.cs 40 KB

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