Skip to content

Add [GeneratedRegex] properties #3690

@jamescrosswell

Description

@jamescrosswell

We've got lots of regexes defined as properties, with backing methods specifically because we had to use methods to leverage the source generated regexes. For example:

#if NET8_0_OR_GREATER
[GeneratedRegex(ValidOriginPattern, RegexOptions.Compiled)]
private static partial Regex ValidOriginRegEx();
private static readonly Regex ValidOrigin = ValidOriginRegEx();
#else
private static readonly Regex ValidOrigin = new (ValidOriginPattern, RegexOptions.Compiled);
#endif
public static bool IsValidOrigin(string? value) => value == null || ValidOrigin.IsMatch(value);
internal static string? TryParse(string origin) => IsValidOrigin(origin) ? origin : null;

We should be able to tidy that code up now that [GeneratedRegex] can also be added to properties.

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions