|
4 | 4 |
|
5 | 5 |
|
6 | 6 | // This file defines an internal class used to throw exceptions in BCL code.
|
7 |
| -// The main purpose is to reduce code size. |
8 |
| -// |
| 7 | +// The main purpose is to reduce code size. |
| 8 | +// |
9 | 9 | // The old way to throw an exception generates quite a lot IL code and assembly code.
|
10 | 10 | // Following is an example:
|
11 | 11 | // C# source
|
|
17 | 17 | // IL_0012: newobj instance void System.ArgumentNullException::.ctor(string,string)
|
18 | 18 | // IL_0017: throw
|
19 | 19 | // which is 21bytes in IL.
|
20 |
| -// |
| 20 | +// |
21 | 21 | // So we want to get rid of the ldstr and call to Environment.GetResource in IL.
|
22 | 22 | // In order to do that, I created two enums: ExceptionResource, ExceptionArgument to represent the
|
23 |
| -// argument name and resource name in a small integer. The source code will be changed to |
| 23 | +// argument name and resource name in a small integer. The source code will be changed to |
24 | 24 | // ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key, ExceptionResource.ArgumentNull_Key);
|
25 | 25 | //
|
26 | 26 | // The IL code will be 7 bytes.
|
|
29 | 29 | // IL_000a: call void System.ThrowHelper::ThrowArgumentNullException(valuetype System.ExceptionArgument)
|
30 | 30 | // IL_000f: ldarg.0
|
31 | 31 | //
|
32 |
| -// This will also reduce the Jitted code size a lot. |
| 32 | +// This will also reduce the Jitted code size a lot. |
| 33 | +// |
| 34 | +// It is very important we do this for generic classes because we can easily generate the same code |
| 35 | +// multiple times for different instantiation. |
33 | 36 | //
|
34 |
| -// It is very important we do this for generic classes because we can easily generate the same code |
35 |
| -// multiple times for different instantiation. |
36 |
| -// |
37 | 37 |
|
38 | 38 | using System.Collections.Generic;
|
39 | 39 | using System.Diagnostics;
|
@@ -87,6 +87,12 @@ internal static void ThrowIndexArgumentOutOfRange_NeedNonNegNumException()
|
87 | 87 | ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);
|
88 | 88 | }
|
89 | 89 |
|
| 90 | + internal static void ThrowValueArgumentOutOfRange_NeedNonNegNumException() |
| 91 | + { |
| 92 | + throw GetArgumentOutOfRangeException(ExceptionArgument.value, |
| 93 | + ExceptionResource.ArgumentOutOfRange_NeedNonNegNum); |
| 94 | + } |
| 95 | + |
90 | 96 | internal static void ThrowLengthArgumentOutOfRange_ArgumentOutOfRange_NeedNonNegNum()
|
91 | 97 | {
|
92 | 98 | throw GetArgumentOutOfRangeException(ExceptionArgument.length,
|
|
0 commit comments