StringSyntaxAttribute for syntax highlighting

Leverage the new StringSyntaxAttribute to enhance the developer experience

Home DailyDrop

Daily Knowledge Drop

A new attribute StringSyntaxAttribute, being introduced in .NET7, allows for marking a C# string as containing a specific type of value. This results in:

  • Syntax highlighting for the specific type
  • Basic validation for the specific type.

At the time of this post, this feature is only available in .NET7 preview 1, and when using Visual Studio 2022 17.2.0 Preview 1.0. This may change between now and .NET7 release.


Core libraries

As part of .NET7, the core .NET libraries have been updated, where relevant, to make use of the new attribute.

Currently there are 3 supported types/languages, but the framework is extensible so more languages can be added:

  • Regex
  • Json
  • DateTime

Most, if not all, methods in the core libraries which accept a string parameter of one of these types, now makes use of the attribute.

This allows for full syntax highlighting:

Regex and Json example

In the above example, you can see how:

  • A string being passes to the Regex constructor has syntax highlighting
  • A bit more subtle, but the string representing a json value being passed to JsonSerializer.Deserialize also has syntax highlighting
  • A string representing a json value being passed to a normal method does not have any syntax highlighting

In the case of DateTime, the attribute provides additional intellisense assistance:

DateTime information

By marking a string with the attribute, basic validation is also applied, based on the language type:

JSON syntax error/warning

The above string has malformed json (missing quote), but has been marked as containing a json value - Roslyn flags this with a warning to highlight the string has an invalid value according to the specific type.


Non-attribute highlighting

It is also possible to mark a normal string with syntax highlighting, outside of using the attribute. This is done by prefixing the string with /*lang=xxx*/ (where xxx is the language)

Non-attribute usage

Here, a normal string declaration has been marked as containing regex formatting and the string has been highlighted accordingly.

A string parameter (not marked with StringSyntaxAttribute) has also had the syntax highlighting applied by using the /*lang=regex*/ method.


Own libraries

It is also possible to make use of the attribute in our own code, and mark string parameters as being of a certain type. This allows for a better developer experience for users of the libraries/methods:

Attribute usage in own method

And an example with json

Attribute usage with json string


Notes

This is an amazing and incredibly useful feature, especially for the developers who work with the supported languages/types (Regex, JSON and DateTime at present). With this being extensible, I am excited to see where this goes and the supported languages increase!


References

.NET Tooling Community Standup - .NET Performance sneak peek!

Daily Drop 39: 28-03-2022

At the start of 2022 I set myself the goal of learning one new coding related piece of knowledge a day.
It could be anything - some.NET / C# functionality I wasn't aware of, a design practice, a cool new coding technique, or just something I find interesting. It could be something I knew at one point but had forgotten, or something completely new, which I may or may never actually use.

The Daily Drop is a record of these pieces of knowledge - writing about and summarizing them helps re-enforce the information for myself, as well as potentially helps others learn something new as well.
c# .net attribute syntax