ThrowHelper.cs 37 KB

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