All rules
CA1418Portability and interoperability Enabled by default: As warning

Validate platform compatibility

Validate platform compatibility

Microsoft docs

Description

The platform compatibility attributes derived from System.Runtime.Versioning.OSPlatformAttribute use string literals for operating system (OS) platform names with an optional version part. The string should consist of a known platform name and either no version part or a valid version part.

The known platform names list is populated from two places:

  • The PlatformName part of System.OperatingSystem guard methods named OperatingSystem.Is<PlatformName>[VersionAtLeast](). For example, guard method System.OperatingSystem.IsWindows adds Windows to the known platform names list.
  • The project's MSBuild item group of SupportedPlatform items, including the default MSBuild SupportedPlatforms list. This is the project specific knowledge of known platforms. It allows class library authors to add more platforms into the known platforms list. For example:

If the platform string contains a *version* part, it should be a valid System.Version with the following format: major.minor[.build[.revision]].

### Violations

  • Solaris is an unknown platform name because it's not included in the default MSBuild SupportedPlatforms list and there's no guard method named OperatingSystem.IsSolaris() in the System.OperatingSystem class.
  • Android is a known platform because there is a System.OperatingSystem.IsAndroid guard method in the System.OperatingSystem type. However, the version part is not a valid version. It should have at least two integers separated by a dot.
  • Linux is a known platform because it is included in the default MSBuild SupportedPlatforms list, and there's also a guard method named System.OperatingSystem.IsLinux. However, there are no versioned guard methods like System.OperatingSystem.IsLinuxVersionAtLeast(int,int) for the Linux platform, therefore no version part is supported on Linux.

Cause

The platform compatibility analyzer requires a valid platform name and version. Violations are reported if the platform string provided to the System.Runtime.Versioning.OSPlatformAttribute constructor consists of an unknown platform name or if the optional version part is invalid.

How to fix violations

  • Change the platform to a known platform name.
  • If the platform name is correct and you want to make it a known platform, then add it to the MSBuild SupportedPlatforms list in your project file:
  • Fix the invalid version. For example, for Android, 10 is not a valid version, but 10.0 is valid.
  • If the platform doesn't support a version, remove the version part.

Example

  [SupportedOSPlatform("Solaris")] // No warning
  public void SolarisApi() { }

When to suppress

Using an unknown platform name or an invalid version is not recommended, so you shouldn't suppress this rule.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0