From 128f12e1d5c57649867c8c9397dd36be0fc6dd19 Mon Sep 17 00:00:00 2001 From: lihanbo Date: Fri, 14 Feb 2025 14:04:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=95=B0=E6=8D=AE=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E6=A3=80=E6=9F=A5=E9=80=BB=E8=BE=91=EF=BC=8C=E6=8F=90?= =?UTF-8?q?=E5=8D=87=E4=BB=A3=E7=A0=81=E5=8F=AF=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在 `foreach` 循环中,将注释“检查是否为 Int”修改为“检查数据类型”,并将每个数据类型检查的注释移到相应的 `if` 语句之前。将多个 `if` 语句的检查条件从注释中移到代码中,使代码更具可读性。将原本的多个 `if-else` 语句替换为 `switch` 表达式,以简化代码结构和提高可读性。 --- Utils/Excel2Prompt.cs | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/Utils/Excel2Prompt.cs b/Utils/Excel2Prompt.cs index 3350b25..01d3608 100644 --- a/Utils/Excel2Prompt.cs +++ b/Utils/Excel2Prompt.cs @@ -80,39 +80,38 @@ public class Excel2Prompt { var str = item.ToString(); - // 检查是否为 Int + // 检查数据类型 if (!int.TryParse(str, out _)) + { isInt = false; - - // 检查是否为 Long + } if (!long.TryParse(str, out _)) + { isLong = false; - - // 检查是否为 Double + } if (!double.TryParse(str, out _)) + { isDouble = false; - - // 检查是否为 DateTime + } if (!DateTime.TryParse(str, out _)) + { isDateTime = false; - - // 检查是否为 Bool + } if (!bool.TryParse(str, out _)) + { isBool = false; + } } - if (isInt) - return "int"; - else if (isLong) - return "int64"; - else if (isDouble) - return "double"; - else if (isDateTime) - return "datetime"; - else if (isBool) - return "bool"; - else - return "string"; + return true switch + { + bool _ when isInt => "int", + bool _ when isLong => "int64", + bool _ when isDouble => "double", + bool _ when isDateTime => "datetime", + bool _ when isBool => "bool", + _ => "string", + }; } }