一键导入
enforcing-generated-regex
Force Regex to use GeneratedRegex Source Generator attribute instead of runtime Regex construction for compile-time optimization.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Force Regex to use GeneratedRegex Source Generator attribute instead of runtime Regex construction for compile-time optimization.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Force all DTO (Data Transfer Object) types to be declared as record instead of class for immutability and value-based equality.
Force record instantiation via positional constructor with named arguments instead of property initializer syntax for stronger immutability expression.
Enforce branch-based workflow with meaningful Korean commits and PR creation for traceable decision history.
Force JsonSerializerOptions to be declared as static readonly or readonly fields instead of creating new instances inside methods.
Centralize external namespace imports (BCL, FCL, NuGet) in a GlobalUsings.cs file per project, keeping individual files clean.
| name | enforcing-generated-regex |
| description | Force Regex to use GeneratedRegex Source Generator attribute instead of runtime Regex construction for compile-time optimization. |
Regex를 런타임에 new Regex(...) 또는 Regex.IsMatch(pattern) 형태로 생성하면 매번 정규식을 해석하고 컴파일해야 합니다.
[GeneratedRegex] 어트리뷰트를 사용하면 Source Generator가 컴파일 타임에 정규식을 최적화된 코드로 변환하여 성능과 메모리 효율이 크게 향상됩니다.
new Regex(pattern, RegexOptions.Compiled) 대신 [GeneratedRegex(pattern)] 어트리뷰트를 사용합니다[GeneratedRegex]를 사용하는 클래스는 반드시 partial로 선언합니다private static partial Regex MethodName(); 형태입니다MethodName() (메서드 호출)으로 사용합니다RegexOptions가 필요하면 어트리뷰트 두 번째 인자로 전달합니다// 런타임에 Regex를 생성 - 매번 해석/컴파일 비용 발생
public sealed class Validator
{
private static readonly Regex EmailPattern = new(@"^[\w.+-]+@[\w-]+\.[\w.]+$", RegexOptions.Compiled);
private static readonly Regex PhonePattern = new(@"^\d{2,3}-\d{3,4}-\d{4}$", RegexOptions.Compiled);
public bool IsValidEmail(string email) => EmailPattern.IsMatch(email);
public bool IsValidPhone(string phone) => PhonePattern.IsMatch(phone);
}
// GeneratedRegex로 컴파일 타임 최적화 - Source Generator가 코드 생성
public sealed partial class Validator
{
[GeneratedRegex(@"^[\w.+-]+@[\w-]+\.[\w.]+$")]
private static partial Regex EmailPattern();
[GeneratedRegex(@"^\d{2,3}-\d{3,4}-\d{4}$")]
private static partial Regex PhonePattern();
public bool IsValidEmail(string email) => EmailPattern().IsMatch(email);
public bool IsValidPhone(string phone) => PhonePattern().IsMatch(phone);
}
// Worst Case
private static readonly Regex NamePattern = new(@"^[a-z]+$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
// Best Case
[GeneratedRegex(@"^[a-z]+$", RegexOptions.IgnoreCase)]
private static partial Regex NamePattern();
new Regex(...) 인스턴스 생성static readonly Regex 필드Regex.IsMatch(input, pattern) 정적 호출 (반복 사용 시)