【问题标题】:How to fix C# Warning CA1416 in vscode?如何修复 vscode 中的 C# 警告 CA1416?
【发布时间】:2021-03-21 16:37:12
【问题描述】:

我刚刚开始学习 C#,关注 Youtube 上的 Brackeys。写完后,我在 vscode 中弹出了这个问题:

{
"resource": "/d:/OneDrive/Programming/Youtube/brackeys/How To Program In C#/Basics/Program.cs",
"owner": "msCompile",
"code": "CA1416",
"severity": 4,
"message": "This call site is reachable on all platforms. 'Console.WindowHeight.set' is only supported on: 'windows'. [D:\\OneDrive\\Programming\\Youtube\\brackeys\\How To Program In C#\\Basics\\Basics.csproj]",
"startLineNumber": 11,
"startColumn": 13,
"endLineNumber": 11,
"endColumn": 13
}

我发现这个Microsoft article 正在谈论这个警告,但如果真的是这样,我不明白解决方案:(...

我有一个简单的程序,只是学习控制台类改变终端高度和字体颜色等:

    using System;
    
    namespace Basics
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.Title = "Skynet";
                Console.ForegroundColor = ConsoleColor.Green;
                Console.WindowHeight = 40;
    
                Console.WriteLine();
    
                Console.ReadKey();
    
            }
        }
    }

有人知道如何解决这个问题吗?

【问题讨论】:

    标签: c# visual-studio-code


    【解决方案1】:

    所以错误是关于这一行的:

    Console.WindowHeight = 40;
    

    您尝试设置窗口高度,这是一个用[SupportedOSPlatform("windows")] 属性修饰的方法。

    为了告诉应用程序只在 Windows 中包装方法时执行这一行。

    if (OperatingSystem.IsWindows())
    {
      Console.WindowHeight = 40;
    }
    

    编译器会识别到这一点并停止抛出该注释。

    【讨论】:

    • 哦,现在我明白了!非常感谢您的时间和帮助!你让我很容易正确理解:D.
    猜你喜欢
    • 2010-10-30
    • 1970-01-01
    • 2022-11-28
    • 2021-10-27
    • 2019-08-08
    • 2020-11-28
    • 2017-10-25
    • 2017-07-03
    • 1970-01-01
    相关资源
    最近更新 更多