gpt4 book ai didi

c# - 处理 .NET 核心构建的错误 CA1416 的正确方法是什么?

转载 作者:行者123 更新时间:2023-12-03 16:32:07 28 4
gpt4 key购买 nike

这是我的 C# 代码片段:

if (Environment.IsWindows) {
_sessionAddress = GetSessionBusAddressFromSharedMemory();
}
...
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
private static string GetSessionBusAddressFromSharedMemory() {
...
}
当我运行构建时,我收到一个错误:
error CA1416: 'GetSessionBusAddressFromSharedMemory()' is supported on 'windows' 
我的逻辑是仅当我在 Windows 上时才调用该方法。在 Ubuntu 上构建时如何关闭此警告?问候。

最佳答案

您可以使用预处理器指令来确保该方法仅在编译时在 Windows 中可见:

#if Windows
private static string GetSessionBusAddressFromSharedMemory()
{
...
}
#endif
要定义您需要更新 csproj 的指令,如下所示:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<IsWindows Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Windows)))' == 'true'">true</IsWindows>
<IsOSX Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::OSX)))' == 'true'">true</IsOSX>
<IsLinux Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::IsOSPlatform($([System.Runtime.InteropServices.OSPlatform]::Linux)))' == 'true'">true</IsLinux>
</PropertyGroup>
<PropertyGroup Condition="'$(IsWindows)'=='true'">
<DefineConstants>Windows</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(IsOSX)'=='true'">
<DefineConstants>OSX</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(IsLinux)'=='true'">
<DefineConstants>Linux</DefineConstants>
</PropertyGroup>
</Project>

关于c# - 处理 .NET 核心构建的错误 CA1416 的正确方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65165941/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com