Skip to content

Introduce pseudo-attributes page #42850

@jnm2

Description

@jnm2

Type of issue

Missing information

Description

There is a class of attributes interpreted by the compiler which could have its own page. I've seen them described as pseudo-custom attributes, or, as https://learn.microsoft.com/en-us/dotnet/api/system.reflection.assembly.getcustomattributes describes them in the table it contains, simply pseudo-attributes. A pseudo-attribute is something that appears in C# syntax as an ordinary attribute, but upon compilation, it is not emitted into the IL custom attributes table as an ordinary attribute would be. Instead, IL flags or options are created. By making these IL options look like attributes, these options can be controlled using the C# language.

All of these may be used in C# source.

ComImportAttribute: maps to the IL import modifier
DefaultParameterValueAttribute: maps to the default value for the parameter
DllImportAttribute: maps to the IL pinvokeimpl modifier, with various options
FieldOffsetAttribute: maps to the offset for the IL .field
InAttribute: maps to the IL [in] modifier
MarshalAsAttribute: maps to the IL marshal modifier, with various options
MethodImplAttribute: maps to the IL flag modifier, with specific named flags such as aggressiveinlining or forwardref, as well as native, managed, or optil modifiers for the MethodCodeType field.
NonSerializedAttribute: maps to the IL notserialized modifier
OptionalAttribute: maps to the IL [opt] modifier
OutAttribute: maps to the IL [out] modifier
PreserveSigAttribute: maps to the IL preservesig modifier
SerializableAttribute: maps to the IL serializable modifier
SkipLocalsInitAttribute: maps to the absence of the IL init modifier for .locals (This might belong here rather than in https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/attributes/general#skiplocalsinit-attribute. It's also the odd one out, where a custom attribute is also added to the compilation, but this may be considered an oversight and possibly corrected in the future.)
SpecialNameAttribute maps the IL specialname modifier
StructLayoutAttribute: maps to the IL auto, sequential, or explicit modifiers, and some other options

Page URL

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/attributes/general

Content source URL

https://github.com/dotnet/docs/blob/main/docs/csharp/language-reference/attributes/general.md

Document Version Independent Id

6a60a627-c59c-74de-ce5b-6f52e15e7668

Article author

@BillWagner

Metadata

  • ID: d415a929-c112-ab86-cf2e-13bf2ef64e1a
  • Service: dotnet-csharp
  • Sub-service: lang-reference

Associated WorkItem - 350765

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions