fil*_*nic 4 .net c# 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>
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我怎样才能表明dotnet run这个项目是仅限 Windows 的?
您可以使用System.Runtime.Versioning.SupportedOSPlatformAttribute例如标记每个特定于 Windows 的方法
[SupportedOSPlatform("windows")]
public static void Foo()
=> Thread.CurrentThread.SetApartmentState(ApartmentState.STA);
Run Code Online (Sandbox Code Playgroud)
在 AssemblyInfo 中标记整个程序集(如果有)
[assembly: System.Runtime.Versioning.SupportedOSPlatformAttribute("windows")]
Run Code Online (Sandbox Code Playgroud)
或编辑.csproj文件以针对 Windows 特定版本的 dotnet 使这些更改自动
<TargetFramework>net5.0-windows</TargetFramework>
Run Code Online (Sandbox Code Playgroud)
删除了 AssemblyInfo.cs 文件。
从 .csproj 文件中删除了以下内容
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
添加到 .csproj 文件
<PropertyGroup>
<TargetFramework>net8.0-windows</TargetFramework>
<PlatformName>windows</PlatformName>
<OutputType>WinExe</OutputType>"
Run Code Online (Sandbox Code Playgroud)
</项目>