.net CA1416.如何告诉构建者唯一的平台是Windows?

0yg35tkg  于 2023-03-20  发布在  .NET
关注(0)|答案(2)|浏览(178)

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专用的?

mspsb9vt

mspsb9vt1#

您可以使用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>
w3nuxt5m

w3nuxt5m2#

1.已删除AssemblyInfo.cs文件。
1.从.csproj文件中删除了以下内容
< GenerateAssemblyInfo>假的</ GenerateAssemblyInfo>
1.添加到.csproj文件

<PropertyGroup>

    <TargetFramework>net8.0-windows</TargetFramework>

    <PlatformName>windows</PlatformName>

    <OutputType>WinExe</OutputType>"

</ Project>

相关问题