Skip to content

Update GeneratedRegex to support partial properties #77797

@stephentoub

Description

@stephentoub

Partial properties are destined for support in C# 13. When that happens, we should utilize them in the regex generator (we wanted this experience from the get-go).

This will entail:

  • Updating GeneratedRegexAttribute to target properties:
-[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
+[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, AllowMultiple = false, Inherited = false)]
 public sealed class GeneratedRegexAttribute : Attribute
  • Updating the generator to support instance and static get-only properties, e.g. if a user writes:
[GeneratedRegex("...")]
private partial Regex MyProp1 { get; }

[GeneratedRegex("...")]
public static partial Regex MyProp2 { get; }

the generator emits:

private partial global::System.Text.RegularExpressions.Regex MyProp1 => global::System.Text.RegularExpressions.Generated.MyProp1_0.Instance;
public static partial global::System.Text.RegularExpressions.Regex MyProp2 => global::System.Text.RegularExpressions.Generated.MyProp2_0.Instance;

We will only support get-only auto props. Others will result in an error diagnostic.

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Text.RegularExpressionsin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions