简体   繁体   English

C# 中的 8 个条件的模式匹配:File.Exists 返回 false,但文件确实存在

[英]Pattern Matching with 8 conditions in C#: File.Exists returns false, but the file does exist

Consider the program from below.从下面考虑这个程序。 The idea should be as follows: In the Main method, AreAllArgumentsPassedAndValid() is called that checks certain parameters for correctness.思路应该如下:在Main方法中, AreAllArgumentsPassedAndValid()来检查某些参数的正确性。 The check is carried out using so-called tuple matching.使用所谓的元组匹配进行检查。 A total of 7 + 1 conditions should be checked.总共应检查 7 + 1 个条件。 For the sake of simplicity, I have replaced 7 of these conditions with boolean variables with permanently assigned values, because I am sure that they will be checked correctly.为了简单起见,我将其中的 7 个条件替换为具有永久分配值的 boolean 变量,因为我确信它们会被正确检查。 I only left the last condition as it appears in my original program.我只留下了原始程序中出现的最后一个条件。 In this last condition, the string is checked whether it is not empty or zero and then checked whether the file exists (the string thus represents the path).在最后一个条件中,检查字符串是否为空或不为零,然后检查文件是否存在(字符串因此表示路径)。

The problem: I have found that the file is never found if I have at least 8 conditions in my switch.问题:我发现如果我的交换机中至少有 8 个条件,则永远找不到该文件。 But: As soon as I delete a condition from the switch, for example, cond7 , I only have 7 conditions in total and in this case the file is correctly searched for and a correct value is returned, depending on whether the file exists or not.但是:一旦我从开关中删除一个条件,例如cond7 ,我总共只有 7 个条件,在这种情况下,文件被正确搜索并返回正确的值,具体取决于文件是否存在.

It also doesn't matter where I put my file - I tried to put the file directly under C:\ .我把文件放在哪里也没关系 - 我试图把文件直接放在C:\下。 It is not found there either.那里也找不到。 But if I call File.Exists separately, not in the return/switch, the search for the file works correctly.但是,如果我单独调用File.Exists ,而不是在返回/开关中,则对文件的搜索工作正常。

My question is the following: What influence does the number of arguments in return/switch have on the correct work of File.Exists(oFileName) ?我的问题如下:返回/切换中 arguments 的数量对File.Exists(oFileName)的正确工作有什么影响? I've tried debugging the File.Exists(oFileName) method when I have 8 conditions in the switch, but that didn't bring me up to the solution.当我在开关中有 8 个条件时,我尝试调试File.Exists(oFileName)方法,但这并没有让我找到解决方案。

Information about my PCs: All of them have Win 10 x64 and the same .NET version.关于我的 PC 的信息:它们都有 Win 10 x64 和相同的 .NET 版本。 And on all of them is the issue the same.所有这些问题都是一样的。

The whole program with 8 conditions in the return/switch:整个程序在返回/切换中有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
                    };
            }
        }
    }
}

The AreAllArgumentsPassedAndValid method with only 7 conditions - in this case the program works as it should: 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
                };
}

When creating a tuple with 8 values, it starts to get weird .当创建一个有 8 个值的元组时,它开始变得很奇怪 The better approach is to create an object with named properties, since I'd argue the tuple you have is impossible to read.更好的方法是创建一个具有命名属性的 object,因为我认为您拥有的元组是无法读取的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM