You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: proposals/caller-argument-expression.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@ Allow developers to capture the expressions passed to a method, to enable better
15
15
16
16
When an assertion or argument validation fails, the developer wants to know as much as possible about where and why it failed. However, today's diagnostic APIs do not fully facilitate this. Consider the following method:
17
17
18
-
```cs
18
+
```csharp
19
19
TSingle<T>(thisT[] array)
20
20
{
21
21
Debug.Assert(array!=null);
@@ -31,7 +31,7 @@ This is also the reason testing frameworks have to provide a variety of assert m
31
31
32
32
While the situation is a bit better for argument validation because the names of invalid arguments are shown to the developer, the developer must pass these names to exceptions manually. If the above example were rewritten to use traditional argument validation instead of `Debug.Assert`, it would look like
33
33
34
-
```cs
34
+
```csharp
35
35
TSingle<T>(thisT[] array)
36
36
{
37
37
if (array==null)
@@ -55,7 +55,7 @@ Notice that `nameof(array)` must be passed to each exception, although it's alre
55
55
56
56
In the above examples, including the string `"array != null"` or `"array.Length == 1"` in the assert message would help the developer determine what failed. Enter `CallerArgumentExpression`: it's an attribute the framework can use to obtain the string associated with a particular method argument. We would add it to `Debug.Assert` like so
@@ -167,7 +167,7 @@ There should always be an expression corresponding to the `this` parameter. Even
167
167
- If being able to see source code at call sites for methods that use this attribute proves to be a problem, we can make the attribute's effects opt-in. Developers will enable it through an assembly-wide `[assembly: EnableCallerArgumentExpression]` attribute they put in `AssemblyInfo.cs`.
168
168
- In the case the attribute's effects are not enabled, calling methods marked with the attribute would not be an error, to allow existing methods to use the attribute and maintain source compatibility. However, the attribute would be ignored and the method would be called with whatever default value was provided.
- To prevent the [binary compatibility problem][drawbacks] from occurring every time we want to add new caller info to `Debug.Assert`, an alternative solution would be to add a `CallerInfo` struct to the framework that contains all the necessary information about the caller.
0 commit comments