With the release of .NET 7, a new attribute was introduced; the StringSyntaxAttribute
.
What does the StringSyntaxAttribute do?
The StringSyntaxAttribute
allows you to tag strings. It can be applied to fields, properties, and parameters and provides tools like Visual Studio with information about the nature of the string.
The compiler will use this information to provide the correct syntax highlighting:
In this release following string formats can be used out of the box (and it is possible to create custom ones):
- CompositeFormat: The syntax identifier for strings containing composite formats for string formatting.
- DateOnlyFormat: The syntax identifier for strings containing date format specifiers.
- DateTimeFormat: The syntax identifier for strings containing date and time format specifiers.
- EnumFormat: The syntax identifier for strings containing Enum format specifiers.
- GuidFormat: The syntax identifier for strings containing Guid format specifiers.
- Json: The syntax identifier for strings containing JavaScript Object Notation (JSON).
- NumericFormat: The syntax identifier for strings containing numeric format specifiers.
- Regex: The syntax identifier for strings containing regular expressions.
- TimeOnlyFormat: The syntax identifier for strings containing time format specifiers.
- TimeSpanFormat: The syntax identifier for strings containing TimeSpan format specifiers.
- Uri: The syntax identifier for strings containing URIs.
- Xml: The syntax identifier for strings containing XML.
Remark: Not all of the above formats are recognized by Visual Studio at the moment of writing.