using System; using System.Text; using System.Text.RegularExpressions; using CodePlay.Core.Interfaces; namespace CodePlay.Core.Pipeline.Converters; /// /// 属性转方法转换器 - { get; set; } → 私有字段 + getter/setter /// public class PropertyConverter : ILineConverter { public int Priority => 70; public string Convert(string line, ConversionContext context) { var propMatch = Regex.Match(line, @"^(public|private|protected)\s+(static\s+)?(readonly\s+)?(\w+(?:<[^>]+>)?)\s+(\w+)\s*\{\s*get;\s*set;\s*\}$"); if (!propMatch.Success) { // 尝试匹配 init-only — 使用与普通属性相同的组索引 propMatch = Regex.Match(line, @"^(public|private|protected)\s+(static\s+)?(\s+)?(\w+(?:<[^>]+>)?)\s+(\w+)\s*\{\s*get;\s*init;\s*\}$"); } if (!propMatch.Success) return line; var access = propMatch.Groups[1].Value; var staticMod = propMatch.Groups[2].Success ? "static " : ""; var readonlyMod = propMatch.Groups[3].Success ? "final " : ""; var type = propMatch.Groups[4].Value; var name = propMatch.Groups[5].Value; var camelName = char.ToLowerInvariant(name[0]) + name.Substring(1); var sb = new StringBuilder(); sb.AppendLine($"private {staticMod}{readonlyMod}{type} {camelName};"); sb.AppendLine($"{access} {staticMod}{type} get{name}() {{ return {camelName}; }}"); sb.AppendLine($"{access} {staticMod}void set{name}({type} value) {{ this.{camelName} = value; }}"); return sb.ToString().TrimEnd(); } }