【问题标题】:Console.WriteLine() output truncated messages or in a wrong order/incompleteConsole.WriteLine() 输出截断消息或顺序错误/不完整
【发布时间】:2019-01-21 01:08:19
【问题描述】:

嗯,我不知道为什么会这样,但这看起来很奇怪,因为我没有找到类似的东西:

你可以看到检查完整的字符串,我们可以看到整个输出,但在控制台中它看起来被截断了。是一样的:

你注意到了吗?查看前 4 行。并查看检查员。缺少一些tuple.Item1yuple.Item2

您可以看到 LINQ 选择有问题。但我不知道为什么会这样。

在那里你可以看到整个代码:

    private const string ClassName = "ClassName",
                         InheritedClass = "InheritedClass";

    private static void Main(string[] args)
    {
        var files = Directory.GetFiles(StaticPath, "*.cs", SearchOption.AllDirectories);

        var classes = files.Select(file => GetClassesInFile(file)).SelectMany(c => c).ToList();

        string fullMsg = string.Join(Environment.NewLine, classes.Select(tuple => $"{tuple.Item1}{(tuple.Item2.HasContent(string.Empty, " ") ? " : " + string.Join(", ", tuple.Item2) + " " : " ")}(on {Path.GetFileName(tuple.Item3)})"));
        Console.WriteLine(fullMsg);

        var unsealedClasses = classes.Where(c => !classes.Any(subClass => subClass.Item2.Contains(c.Item1))).ToList();

        string msg = $"Unsealed classes count: {unsealedClasses.Count} ({string.Join(", ", unsealedClasses.Select(c => c.Item1))})";
        Console.WriteLine(msg);

        Console.Read();
    }

    private static IEnumerable<Tuple<string, string[], string>> GetClassesInFile(string file)
    {
        return Regex.Matches(File.ReadAllText(file), $"(public|internal|private) class (?<{ClassName}>(.+?))( : (?<{InheritedClass}>(.+?))|)\n")
            .Cast<Match>()
            .Select(m => new Tuple<string, string[], string>(GetGroupValue(m, ClassName), GetGroupValue(m, InheritedClass).CleanLineBreak().Split(','), file))
            .Where(t => !string.IsNullOrEmpty(t.Item1));
    }

    private static string GetGroupValue(Match m, string groupName)
    {
        return m.Groups[groupName].Value.ToString();
    }
  • GetGroupValue 从 Regex Group 索引器输出一个字符串。
  • GetClassesInFile 输出一个 IEnumerable 的 Tuple,其中 Item1 是类名,Item2 是它所依赖的类/接口,第三个 Item 是这个类的文件路径。

注意:如您所见,我还在 tuple.Item1 上过滤 null 或空字符串。

我不知道这里发生了什么。但这太奇怪了,我第一次看到。

编辑:

我查看了用户评论中提到的输出高度(参考:Is there a limit on the output of Console Window?),我的输出超过了 9001:

【问题讨论】:

    标签: c# output console.writeline


    【解决方案1】:

    GameMovieClipSpriteTex 类名末尾有回车符 \r。尝试在GetGroupValue方法中调用Trim()

    private static string GetGroupValue(Match m, string groupName)
    {
        return m.Groups[groupName].Value.ToString().Trim();
    }
    

    【讨论】:

    猜你喜欢
    • 2012-09-03
    • 2012-09-12
    • 2012-05-24
    • 2011-12-07
    • 1970-01-01
    • 2016-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多