Skip to content

Commit 12333dc

Browse files
RexJaeschkeBillWagner
authored andcommitted
add the new attribute
1 parent 613c188 commit 12333dc

1 file changed

Lines changed: 71 additions & 1 deletion

File tree

standard/attributes.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ A number of attributes affect the language in some way. These attributes include
497497
- `System.Diagnostics.ConditionalAttribute` ([§23.5.3](attributes.md#2353-the-conditional-attribute)), is a multi-use attribute class which is used to define conditional methods and conditional attribute classes. This attribute indicates a condition by testing a conditional compilation symbol.
498498
- `System.ObsoleteAttribute` ([§23.5.4](attributes.md#2354-the-obsolete-attribute)), which is used to mark a member as obsolete.
499499
- `System.Runtime.CompilerServices.AsyncMethodBuilderAttribute` ([§23.5.5](attributes.md#2355-the-asyncmethodbuilder-attribute)), which is used to establish a task builder for an async method.
500-
- `System.Runtime.CompilerServices.CallerLineNumberAttribute` ([§23.5.6.2](attributes.md#23562-the-callerlinenumber-attribute)), `System.Runtime.CompilerServices.CallerFilePathAttribute` ([§23.5.6.3](attributes.md#23563-the-callerfilepath-attribute)), and `System.Runtime.CompilerServices.CallerMemberNameAttribute` ([§23.5.6.4](attributes.md#23564-the-callermembername-attribute)), which are used to supply information about the calling context to optional parameters.
500+
- `System.Runtime.CompilerServices.CallerLineNumberAttribute` ([§23.5.6.2](attributes.md#23562-the-callerlinenumber-attribute)), `System.Runtime.CompilerServices.CallerFilePathAttribute` ([§23.5.6.3](attributes.md#23563-the-callerfilepath-attribute)), `System.Runtime.CompilerServices.CallerMemberNameAttribute` ([§23.5.6.4](attributes.md#23564-the-callermembername-attribute)), and `System.Runtime.CompilerServices.CallerArgumentExpressionAttribute` (§callargexpattr), which are used to supply information about the calling context to optional parameters.
501501
- `System.Runtime.CompilerServices.EnumeratorCancellationAttribute` ([§23.5.8](attributes.md#2358-the-enumeratorcancellation-attribute)), which is used to specify parameter for the cancellation token in an asynchronous iterator.
502502
- `System.Runtime.CompilerServices.ModuleInitializer` ([§23.5.9](attributes.md#2359-the-moduleinitializer-attribute)), which is used to mark a method as a module initializer.
503503
@@ -881,6 +881,76 @@ For an invocation that occurs within a local function or an anonymous function,
881881
>
882882
> This attribute supplies the name of the calling function member, which for local function `F1` is the method `Main`. And even though `F2` is called by `F1`, a local function is *not* a function member, so the reported caller of `F2` is also `Main`. *end example*
883883
884+
#### §callargexpattr The CallerArgumentExpression attribute
885+
886+
The attribute `System.Runtime.CompilerServices.CallerArgumentExpression` is applied to a *target parameter*, and can result in the capture of the source-code text of a sibling parameter’s argument as a string, referred to here as the *captured string*.
887+
888+
The target parameter shall have a *default_argument*.
889+
890+
Consider the following method declaration:
891+
892+
<!-- Example: {template:"standalone-lib-without-using", name:"CallerArgumentAttr1"} -->
893+
```csharp
894+
using System;
895+
using System.Runtime.CompilerServices;
896+
#nullable enable
897+
class Test
898+
{
899+
public static void M(int val = 0, [CallerArgumentExpression("val")] string? text = null)
900+
{
901+
Console.WriteLine($"val = {val}, text = <{text}>");
902+
}
903+
}
904+
```
905+
906+
in which the target parameter is `text` and the sibling parameter is `val`, whose corresponding argument’s source-code text can be captured in `text` when `M` is called.
907+
908+
The attribute constructor takes an argument of type `string`. That string
909+
910+
- Shall contain the name of a sibling parameter; otherwise, the attribute is ignored.
911+
- Shall omit the leading `@` from a parameter name having that prefix.
912+
913+
A *parameter_list* may contain multiple target parameters.
914+
915+
The type of the target parameter shall have a standard conversion from `string`.
916+
917+
> *Note:* This means no user-defined conversions from `string` are allowed, and in practice means the type of such a parameter must be `string`, `object`, or an interface implemented by `string`. *end note*
918+
919+
If an explicit argument is passed for the target parameter, no string is captured, and that parameter takes on that arguments value. Otherwise, the text for the argument corresponding to the sibling parameter is converted to a captured string, according to the following rules:
920+
921+
- Leading and trailing white space is removed.
922+
- All other *input_element*s are retained verbatim (including white space, comments, *Unicode_Escape_Sequence*s, and `@` prefixes on identifiers).
923+
924+
The captured string is then passed as the argument corresponding to the target parameter. However, if the argument for the sibling parameter is omitted, the target parameter takes on its *default_argument* value.
925+
926+
> *Example*: Given the declaration of `M` above, consider the following calls to `M`:
927+
>
928+
> <!-- Example: {template:"standalone-console", name:"CallerArgumentAttr2", inferOutput:true, additionalFiles:["CallerArgumentAttrM.cs"]} -->
929+
> ```csharp
930+
> Test.M();
931+
> Test.M(123);
932+
> Test.M(123, null);
933+
> Test.M(123, "xyz");
934+
> Test.M( 1 + 2 );
935+
> int local = 10;
936+
> Test.M(l\u006fcal /*...*/ + // xxx
937+
> 5);
938+
> ```
939+
>
940+
> the output produced is
941+
>
942+
> ```console
943+
> val = 0, text = <>
944+
> val = 123, text = <123>
945+
> val = 123, text = <>
946+
> val = 123, text = <xyz>
947+
> val = 3, text = <1 + 2>
948+
> val = 15, text = <l\u006fcal /*...*/ + // xxx
949+
> 5>
950+
> ```
951+
>
952+
> *end example*
953+
884954
### 23.5.7 Code analysis attributes
885955
886956
#### 23.5.7.1 General

0 commit comments

Comments
 (0)