ThrowHelper.cs 37 KB

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