优化数据类型检查逻辑,提升代码可读性

在 `foreach` 循环中,将注释“检查是否为 Int”修改为“检查数据类型”,并将每个数据类型检查的注释移到相应的 `if` 语句之前。将多个 `if` 语句的检查条件从注释中移到代码中,使代码更具可读性。将原本的多个 `if-else` 语句替换为 `switch` 表达式,以简化代码结构和提高可读性。
This commit is contained in:
lihanbo 2025-02-14 14:04:42 +08:00
parent d1d2bebcd1
commit 128f12e1d5
1 changed files with 20 additions and 21 deletions

View File

@ -80,39 +80,38 @@ public class Excel2Prompt
{ {
var str = item.ToString(); var str = item.ToString();
// 检查是否为 Int // 检查数据类型
if (!int.TryParse(str, out _)) if (!int.TryParse(str, out _))
{
isInt = false; isInt = false;
}
// 检查是否为 Long
if (!long.TryParse(str, out _)) if (!long.TryParse(str, out _))
{
isLong = false; isLong = false;
}
// 检查是否为 Double
if (!double.TryParse(str, out _)) if (!double.TryParse(str, out _))
{
isDouble = false; isDouble = false;
}
// 检查是否为 DateTime
if (!DateTime.TryParse(str, out _)) if (!DateTime.TryParse(str, out _))
{
isDateTime = false; isDateTime = false;
}
// 检查是否为 Bool
if (!bool.TryParse(str, out _)) if (!bool.TryParse(str, out _))
{
isBool = false; isBool = false;
}
} }
if (isInt) return true switch
return "int"; {
else if (isLong) bool _ when isInt => "int",
return "int64"; bool _ when isLong => "int64",
else if (isDouble) bool _ when isDouble => "double",
return "double"; bool _ when isDateTime => "datetime",
else if (isDateTime) bool _ when isBool => "bool",
return "datetime"; _ => "string",
else if (isBool) };
return "bool";
else
return "string";
} }
} }