我在 .net 5.0 项目中使用 System.IO.Ports。这会生成如下警告:
warning CA1416: This call site is reachable on all platforms. 'SerialPort.Close()' is unsupported on: 'ios', 'android'.
我可以在调用 SerialPort 方法之前放置防护装置来检查平台,但我在很多地方都调用了这些方法。
相反,我想知道是否有一种方法可以在项目级别告诉.net我不支持ios和android。
我不想想要抑制所有CA1416警告,因为我确实支持:
- Windows 和...
- Linux
...如果我使用这些平台上不可用的内容,希望收到警告。
最佳答案
TL; DR:本质上你的问题分为两部分,遗憾的是似乎只有一个部分是可能的。
1。如何从项目中排除操作系统
操作:
How to globally specify android and ios are not supported in a .net 5.0 app
I can put guards in to check the platform before calling SerialPort methods, but I calls these in a lot of places.
是的。而不是允许所有显示 net5.0
的目的地提供,您可以使用复数 <TargetFrameworks>
微调输出元素(注意“s”)而不是默认的 <TargetFramework>
1。
这是一个针对 net5.0
的示例项目:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
</Project>
上述项目将使您的应用程序能够针对所有操作系统进行编译,包括 Windows、Mac OS、Android 和 iOS。
Instead I was wondering if there was a way to tell .net at the project level that I don't support ios and android.
是的。为了实现这一目标,我们将使用特定于操作系统的 TFM,而不是通用的 net5.0
。 1
- 更改
<TargetFramework>
至<TargetFrameworks>
- 更改
net5.0
至net5.0-windows;net5.0-macos
...像这样:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net5.0-windows;net5.0-macos</TargetFrameworks>
</PropertyGroup>
</Project>
iOS 和 Android 支持已被删除。
2。如何排除Linux
操作:
I don't want to suppress all CA1416 warnings because I do support Windows and Linux
这就是棘手的地方,因为 Microsoft 尚未为 Linux 定义 TFM(太多风格?),因此指定您要针对 Linux 的唯一方法是使用 net5.0
。隐含 Linux 支持。不幸的是,这重新激活对iOS和Android的支持,而这正是我们刚刚删除的。它还激活了您可能不关心的 MacOS。
1 Target frameworks in SDK-style projects ,微软金融科技
关于c# - 如何全局指定 .net 5.0 应用程序不支持 android 和 ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71097598/