Home > database >  How to globally specify android and ios are not supported in a .net 5.0 app
How to globally specify android and ios are not supported in a .net 5.0 app

Time:02-14

I'm using System.IO.Ports in a .net 5.0 project. This generates warnings as follows:

warning CA1416: This call site is reachable on all platforms. 'SerialPort.Close()' is unsupported on: 'ios', 'android'.

I can put guards in to check the platform before calling SerialPort methods, but I calls these in a lot of places.

Instead I was wondering if there was a way to tell .net at the project level that I don't support ios and android.

I don't want to suppress all CA1416 warnings because I do support:

  • Windows and...
  • Linux

...and want to be warned if I use something not available on those platforms.

CodePudding user response:


TL; DR: essentially your question is divided into two parts and sadly only one appears to be possible.


1. How to exclude an OS from your project

OP:

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.

Yes. Instead of allowing all destinations that say net5.0 provides, you can fine-tune your output by using the plural <TargetFrameworks> element (note the "s") instead of the default <TargetFramework>1.

Here's an example project that targeting net5.0:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>
</Project>

The above project will let your app compile for all OSs including Windows, Mac OS, Android and 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.

Yes. To achieve that we would use OS-specific TFMs instead of the general net5.0. 1

  1. change <TargetFramework> to <TargetFrameworks>
  2. change net5.0 to net5.0-windows;net5.0-macos

...like so:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net5.0-windows;net5.0-macos</TargetFrameworks>
  </PropertyGroup>
</Project>

With this iOS and Android support have been removed.

2. How to exclude Linux

OP:

I don't want to suppress all CA1416 warnings because I do support Windows and Linux

This is where it gets tricky as Microsoft has not defined a TFM for Linux (too many flavours?) so the only way to specify that you want to target Linux is to use net5.0. Linux support is implied. Unfortunately this re-activates support for iOS and Android the very thing we just removed. It also activated MacOS which you might not care about.


1 Target frameworks in SDK-style projects , MSFT

CodePudding user response:

Paraphrasing from the warning documentation;

You can specify multiple "target framework moniker" (TFM) in your projects <TargetFramework> or <TargetFrameworks> tags. If the TFM has a target platform, the .NET SDK injects a SupportedOSPlatform attribute with the targeted platform name in the AssemblyInfo.cs file, which is consumed by the analyzer.

The analyzer doesn't seem to care which frameworks you target, just the SupportedOSPlatform / UnsupportedOSPlatform attributes compiled into your assembly.

While you can't specify a set of target frameworks that would allow windows and linux, but nothing else. That doesn't stop you from defining multiple SupportedOSPlatform attributes in your AssemblyInfo.cs;

using System.Runtime.Versioning;

[assembly: SupportedOSPlatform("windows")]
[assembly: SupportedOSPlatform("linux")]

Which I believe should limit how the CA1416 warnings are generated. Even if your project references <TargetFramework>net5.0</TargetFramework>.

  • Related