Validate platform compatibility
Validate platform compatibility
Microsoft docsDescription
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
PlatformNamepart of System.OperatingSystem guard methods namedOperatingSystem.Is<PlatformName>[VersionAtLeast](). For example, guard method System.OperatingSystem.IsWindows addsWindowsto the known platform names list. - The project's MSBuild item group of
SupportedPlatformitems, 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
Solarisis an unknown platform name because it's not included in the default MSBuild SupportedPlatforms list and there's no guard method namedOperatingSystem.IsSolaris()in the System.OperatingSystem class.
Androidis 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.
Linuxis 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 likeSystem.OperatingSystem.IsLinuxVersionAtLeast(int,int)for theLinuxplatform, 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,10is not a valid version, but10.0is 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.