c# - CA1416。如何告诉构建器只有平台是 Windows?

标签 c# .net msbuild .net-5

dotnet run(在 Windows 上)导致warning CA1416: This call site is reachable on all platforms. 'WellKnownSidType.WorldSid' is only supported on: 'windows'. 我的程序设计为只能在 Windows 上运行。
我试图将 SupportedPlatform 添加到 MyApp.csproj ,但没​​有运气。

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Authentication.Negotiate" Version="5.0.5" />
    <PackageReference Include="System.DirectoryServices" Version="5.0.0" />
    <SupportedPlatform Include="Windows"/>
  </ItemGroup>

</Project>
我究竟做错了什么?如何向 dotnet run 显示该项目仅适用于 Windows?

最佳答案

您可以使用 System.Runtime.Versioning.SupportedOSPlatformAttribute 标记每个特定于 Windows 的方法,例如

[SupportedOSPlatform("windows")]
public static void Foo()
    => Thread.CurrentThread.SetApartmentState(ApartmentState.STA);
在 AssemblyInfo 中标记整个程序集(如果有)
[assembly: System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
或者编辑 .csproj 文件以针对 Windows 特定版本的 dotnet 使这些更改自动进行
<TargetFramework>net5.0-windows</TargetFramework>

关于c# - CA1416。如何告诉构建器只有平台是 Windows?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67117053/

相关文章:

asp.net - 如何使用 MSBuild 编译 ASP.Net MVC 项目

c# - .NET7 Azure 函数 : the host has not yet started

c# - 如何处理表达式树中的闭包和静态方法调用?

c# - .NET 计划任务 - WqlEventQuery 每 30 分钟执行一次

.net - 何时设置 HttpContext.User.Identity?

.net - MSDeploy 所有配置 [.config 文件] 在一个包中

c# - 动态对象属性名以数字开头

c# - BasedOn 属性不适用于 ListView

c# - 有没有办法在方法之间进行数学加法?

msbuild - 将属性值从TFS构建定义传递到proj文件