From 20b0f65e6ad3011c98d4c635ef3a49e0d08d7522 Mon Sep 17 00:00:00 2001 From: Oliver Wong Date: Tue, 19 May 2026 14:12:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=A8=B3=E5=81=A5=E6=80=A7=E6=8F=90?= =?UTF-8?q?=E5=8D=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 现在会校验枚举名是否合法并转换为PascalCase --- ExcelTool/Parser/ParsedEnum.cs | 49 ++++++++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 5 deletions(-) diff --git a/ExcelTool/Parser/ParsedEnum.cs b/ExcelTool/Parser/ParsedEnum.cs index c6f4699..70db4b0 100644 --- a/ExcelTool/Parser/ParsedEnum.cs +++ b/ExcelTool/Parser/ParsedEnum.cs @@ -1,11 +1,16 @@ using System.Collections.Generic; +using System.Text.RegularExpressions; +using System.Linq; namespace ExcelTool.Parser { public class ParsedEnumMember { - /// 成员名,如 "Home" - public string Key { get; set; } + public string Key + { + get; + set { field = ParsedEnum.NormalizeIdentifier(value); } + } /// 显式值,为 null 时代码生成时自动递增(不写值) public int? Value { get; set; } @@ -19,9 +24,43 @@ namespace ExcelTool.Parser /// A 列 Id,供外部系统通过 EnumIds 常量类引用 public uint Id { get; set; } - /// 枚举类型名,如 "GameState" - public string EnumName { get; set; } + public string EnumName + { + get; + set + { + field = NormalizeIdentifier(value); + } + } - public List Members { get; set; } = new(); + public static string NormalizeIdentifier(string input) + { + if (string.IsNullOrWhiteSpace(input)) + { + return string.Empty; + } + + string[] parts = Regex.Split(input, @"[\s_-]+") + .Where(s => !string.IsNullOrWhiteSpace(s)) + .ToArray(); + + for (int i = 0; i < parts.Length; i++) + { + string part = parts[i]; + + if (part.Length == 1) + { + parts[i] = char.ToUpperInvariant(part[0]).ToString(); + } + else + { + parts[i] = char.ToUpperInvariant(part[0]) + part.Substring(1); + } + } + + return string.Concat(parts); + } + + public List Members { get; set; } = []; } }