c# - C# 中 8 个条件的模式匹配:File.Exists 返回 false,但文件确实存在
问题描述
从下面考虑这个程序。思路应该是这样的:在Main
方法中,AreAllArgumentsPassedAndValid()
被调用来检查某些参数的正确性。使用所谓的元组匹配进行检查。总共应检查 7 + 1 个条件。为简单起见,我已将其中 7 个条件替换为具有永久赋值的布尔变量,因为我确信它们会被正确检查。我只留下了原始程序中出现的最后一个条件。在最后一个条件中,检查字符串是否为空或不为零,然后检查文件是否存在(字符串因此表示路径)。
问题:我发现如果我的交换机中至少有 8 个条件,则永远找不到该文件。但是:一旦我从开关中删除一个条件,例如,cond7
我总共只有 7 个条件,在这种情况下,文件被正确搜索并返回正确的值,具体取决于文件是否存在。
我把文件放在哪里也没关系 - 我试图把文件直接放在C:\
. 那里也找不到。但是,如果我File.Exists
单独调用,而不是在返回/开关中调用,则对文件的搜索工作正常。
我的问题如下: return/switch 中的参数数量对 的正确工作有什么影响File.Exists(oFileName)
?当我在交换机中有 8 个条件时,我尝试过调试该File.Exists(oFileName)
方法,但这并没有让我找到解决方案。
关于我的 PC 的信息:它们都具有 Win 10 x64 和相同的 .NET 版本。所有这些问题都是一样的。
整个程序在返回/切换中有8个条件:
using System;
using System.IO;
namespace PatternMatchingTest
{
class Program
{
private static class Constants
{
public const string ProgramSide = "PROGRAM_SIDE";
public const string OldFileName = "OLD_FILE_NAME";
public const string NewFileName = "NEW_FILE_NAME";
}
static void Main(string[] args)
{
string oldFilePath = @"C:\Users\ADMINI~1\AppData\Local\Temp\Test.txt";
Console.WriteLine(AreAllArgumentsPassedAndValid());
Console.ReadKey();
string AreAllArgumentsPassedAndValid()
{
string oFileName = oldFilePath;
bool cond1 = true, cond2 = true, cond3 = true, cond4 = true, cond5 = true, cond6 = false, cond7 = false;
return (cond1,
cond2,
cond3,
cond4,
cond5,
cond6,
cond7,
!string.IsNullOrEmpty(oFileName) && File.Exists(oFileName))
switch
{
(false, _, _, _, _, _, _, _) => $"Invalid number of arguments. 3 arguments are expected.",
(_, false, _, _, _, _, _, _) => $"Missing {Constants.ProgramSide} argument.",
(_, _, false, _, _, _, _, _) => $"Missing {Constants.OldFileName} argument.",
(_, _, _, false, _, _, _, _) => $"Missing {Constants.NewFileName} argument.",
(_, _, _, _, false, _, _, _) => $"Argument {Constants.ProgramSide} has invalid value.",
(_, _, _, _, _, true, _, _) => $"Argument {Constants.OldFileName} has invalid value: null or empty. Expected: path to a file.",
(_, _, _, _, _, _, true, _) => $"Argument {Constants.NewFileName} has invalid value: null or empty. Expected: path to a file.",
(_, _, _, _, _, _, _, false) => $"File {oFileName} does not exist.",
(true, true, true, true, true, false, false, true) => string.Empty
};
}
}
}
}
该AreAllArgumentsPassedAndValid
方法只有 7 个条件 - 在这种情况下,程序可以正常工作:
string AreAllArgumentsPassedAndValid()
{
string oFileName = oldFilePath;
bool cond1 = true, cond2 = true, cond3 = true, cond4 = true, cond5 = true, cond6 = false, cond7 = false;
return (cond1,
cond2,
cond3,
cond4,
cond5,
cond6,
!string.IsNullOrEmpty(oFileName) && File.Exists(oFileName))
switch
{
(false, _, _, _, _, _, _) => $"Invalid number of arguments. 3 arguments are expected.",
(_, false, _, _, _, _, _) => $"Missing {Constants.ProgramSide} argument.",
(_, _, false, _, _, _, _) => $"Missing {Constants.OldFileName} argument.",
(_, _, _, false, _, _, _) => $"Missing {Constants.NewFileName} argument.",
(_, _, _, _, false, _, _) => $"Argument {Constants.ProgramSide} has invalid value.",
(_, _, _, _, _, true, _) => $"Argument {Constants.OldFileName} has invalid value: null or empty. Expected: path to a file.",
(_, _, _, _, _, _, false) => $"File {oFileName} does not exist.",
(true, true, true, true, true, false, true) => string.Empty
};
}
解决方案
当创建一个有 8 个值的元组时,它开始变得很奇怪。更好的方法是创建一个具有命名属性的对象,因为我认为你拥有的元组是不可能阅读的。
推荐阅读
- binary - 设计一个计算二进制数奇偶校验的图灵机
- java - 多个服务的 Apache Curator 双重锁定问题
- python - 从 csv 数据框中绘制列的泊松分布的 PMF
- cloud - 没有合适的 ServiceConnectorCreator
- swift - 给定方法名称的字符串表示形式,如何在运行时调用 Swift 5 中的方法?
- r - 使用一些本地项目配置 packrat 作为依赖项(没有任何本地存储库)
- typescript - 无法导出组合索引泛型
- javascript - Express.js POST 请求中的单个项目未定义
- r - 检查R中字符串和单词向量之间的重合
- python - 使用 seaborn 将 y 轴值限制为 1 的问题