Skip to content

Commit

Permalink
Fixes dotnet#43110
Browse files Browse the repository at this point in the history
Add a list of attributes that are written by the compiler that shouldn't be added in users' source code.
  • Loading branch information
BillWagner committed Jan 27, 2025
1 parent 5d90337 commit 5d400e0
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion docs/csharp/language-reference/attributes/pseudo-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,30 @@ You add these attributes to your code for the compiler to emit specified Interme
- <xref:System.Runtime.InteropServices.StructLayoutAttribute?displayProperty=fullName>: Specifies the IL `auto`, `sequential`, or `explicit` modifiers. Layout options can be set using the parameters.
- <xref:System.Runtime.CompilerServices.IndexerNameAttribute?displayProperty=fullName>: You add this attribute to an indexer to set a different method name. By default, indexers are compiled to a property named `Item`. You can specify a different name using this attribute.

Other custom attributes are generally applied using other C# syntax rather than adding the attribute to your source code.
Some of these custom attributes are generally applied using other C# syntax rather than adding the attribute to your source code.

- <xref:System.Runtime.InteropServices.DefaultParameterValueAttribute?displayProperty=fullName>: Specifies the default value for the parameter. Use the [default parameter syntax](../../methods.md#optional-parameters-and-arguments)
- <xref:System.Runtime.InteropServices.InAttribute?displayProperty=fullName>: Specifies the IL `[in]` modifier. Use the [`in`](../keywords/method-parameters.md#in-parameter-modifier) or [`ref readonly`](../keywords/method-parameters.md#ref-readonly-modifier).
- <xref:System.Runtime.CompilerServices.SpecialNameAttribute?displayProperty=fullName>: Specifies the IL `specialname` modifier. The compiler automatically uses the for methods that require the `specialname` modifier.
- <xref:System.Runtime.InteropServices.UnmanagedCallersOnlyAttribute?displayProperty=nameWithType>: This attribute is required for the `delegate*` feature. The compiler adds it to any [`delegate*`](../unsafe-code.md#function-pointers) requires its use.

The following custom attributes are generally disallowed in C# source. They are listed here to aid library authors who use reflection, and to ensure you don't create custom attributes with the same name.

- <xref:System.Runtime.CompilerServices.CompilerFeatureRequiredAttribute?displayProperty=fullName>: Prevents downlevel compilers from using metadata it can't safely understand.
- <xref:System.Runtime.CompilerServices.DecimalConstantAttribute?displayProperty=fullName>: Encodes `const decimal` fields. The runtime doesn't support `decimal` values as constant values.
- <xref:System.Reflection.DefaultMemberAttribute?displayProperty=fullName>: Encodes indexers with [IndexerNameAttribute](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.indexernameattribute). This attributes notes the default indexer when its name has been customized. This attribute is allowed in source.
- <xref:System.Runtime.CompilerServices.DynamicAttribute?displayProperty=fullName>: Encodes whether a type in a signature is `dynamic` (versus `object`).
- <xref:System.Runtime.CompilerServices.ExtensionAttribute?displayProperty=fullName>: This attributes notes extension methods. The compiler also places this attribute on the containing classes.
- <xref:System.Runtime.CompilerServices.FixedBufferAttribute?displayProperty=fullName>: This attribute specifies `fixed` struct fields.
- <xref:System.Runtime.CompilerServices.IsByRefLikeAttribute?displayProperty=fullName>: This attribute specifies a `ref` struct.
- <xref:System.Runtime.CompilerServices.IsReadOnlyAttribute?displayProperty=fullName>: This attribute indicates that a parameter has the `in` modifier. It distinguishes `in` parameters from `readonly ref` or `[In] ref`.
- <xref:System.Runtime.CompilerServices.RequiresLocationAttribute?displayProperty=fullName>: This attributes indicates that a parameter has the `readonly ref` modifier. Ut distinguishes `readonly ref` from `in` or `[In] ref`.
- <xref:System.Runtime.CompilerServices.IsUnmanagedAttribute?displayProperty=fullName> - This attribute specifies the `unmanaged` constraint on a type parameter.
- <xref:System.Runtime.CompilerServices.NullableAttribute?displayProperty=fullName>, <xref:System.Runtime.CompilerServices.NullableContextAttribute?displayProperty=fullName>, <xref:System.Runtime.CompilerServices.NullablePublicOnlyAttribute?displayProperty=fullName>: These attributes encode nullable annotations in your source code.
- <xref:System.ParamArrayAttribute?displayProperty=fullName>: This attribute encodes the `params` modifier on array parameters.
- <xref:System.Runtime.CompilerServices.ParamCollectionAttribute?displayProperty=fullName> This attribute encodes the `params` on non-array parameters.
- <xref:System.Runtime.CompilerServices.RefSafetyRulesAttribute?displayProperty=fullName>: This attributes specifies the C# version that is required in order to understand ref safety annotations in the assembly. Ref safety rules have evolved as new features have been added to the language.
- <xref:System.Runtime.CompilerServices.RequiredMemberAttribute?displayProperty=fullName>: This attribute indicates that the `required` modifier was placed on a member declaration. It's the encoding of the [required members](../keywords/required.md) language feature.
- <xref:System.Runtime.CompilerServices.TupleElementNamesAttribute?displayProperty=fullName>: This attribute encodes tuple element names used in signatures.

In addition, the compiler may generate a declaration for other attributes used internally. For this reason, you should assume other attributes in the <xref:System.Runtime.CompilerServices> namespace. Some aren't in the .NET Runtime, but are generated by the compiler as an `internal` type declaration in any assembly where the attribute is needed.

0 comments on commit 5d400e0

Please sign in to comment.