[英]Pattern Matching with 8 conditions in C#: File.Exists returns false, but the file does exist
从下面考虑这个程序。 思路应该如下:在Main
方法中, AreAllArgumentsPassedAndValid()
来检查某些参数的正确性。 使用所谓的元组匹配进行检查。 总共应检查 7 + 1 个条件。 为了简单起见,我将其中的 7 个条件替换为具有永久分配值的 boolean 变量,因为我确信它们会被正确检查。 我只留下了原始程序中出现的最后一个条件。 在最后一个条件中,检查字符串是否为空或不为零,然后检查文件是否存在(字符串因此表示路径)。
问题:我发现如果我的交换机中至少有 8 个条件,则永远找不到该文件。 但是:一旦我从开关中删除一个条件,例如cond7
,我总共只有 7 个条件,在这种情况下,文件被正确搜索并返回正确的值,具体取决于文件是否存在.
我把文件放在哪里也没关系 - 我试图把文件直接放在C:\
下。 那里也找不到。 但是,如果我单独调用File.Exists
,而不是在返回/开关中,则对文件的搜索工作正常。
我的问题如下:返回/切换中 arguments 的数量对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 个值的元组时,它开始变得很奇怪。 更好的方法是创建一个具有命名属性的 object,因为我认为您拥有的元组是无法读取的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.