ThrowHelper.cs 37 KB

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