如何修复vscode中的C#警告CA1416?

c#

我刚刚开始在Youtube 上跟随Brackeys学习C#。在编写时,我在 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
}

我发现这篇微软文章在谈论这个警告,但我不明白解决方案是否真的是这样:(...

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

    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();
    
            }
        }
    }

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

回答

所以错误是关于这一行:

Console.WindowHeight = 40;

你尝试设置Window Height,这是一个用[SupportedOSPlatform("windows")]属性修饰的方法。

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

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

编译器将识别出这一点并停止抛出该评论。


以上是如何修复vscode中的C#警告CA1416?的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>