A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1416 below:

CA1416: Validate platform compatibility (code analysis) - .NET

Property Value Rule ID CA1416 Title Validate platform compatibility Category Interoperability Fix is breaking or non-breaking Non-breaking Enabled by default in .NET 9 As warning Cause

Violations are reported if a platform-specific API is used in the context of a different platform or if the platform isn't verified (platform-neutral). Violations are also reported if an API that's not supported for the target platform of the project is used.

This rule is enabled by default only for projects that target .NET 5 or later. However, you can enable it for projects that target other frameworks.

Rule description

.NET 5 added new attributes, SupportedOSPlatformAttribute and UnsupportedOSPlatformAttribute, to annotate platform-specific APIs. Both attributes can be instantiated with or without version numbers as part of the platform name. They can also be applied multiple times with different platforms.

You can combine [SupportedOSPlatform] and [UnsupportedOSPlatform] attributes on a single API. In this case, the following rules apply:

If you access an API annotated with these attributes from the context of a different platform, you can see CA1416 violations.

TFM target platforms

The analyzer does not check target framework moniker (TFM) target platforms from MSBuild properties, such as <TargetFramework> or <TargetFrameworks>. 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. For example, if the TFM is net5.0-windows10.0.19041, the SDK injects the [assembly: System.Runtime.Versioning.SupportedOSPlatform("windows10.0.19041")] attribute into the AssemblyInfo.cs file, and the entire assembly is considered to be Windows only. Therefore, calling Windows-only APIs versioned with 7.0 or below would not cause any warnings in the project.

Note

If the AssemblyInfo.cs file generation is disabled for the project (that is, the <GenerateAssemblyInfo> property is set to false), the required assembly level SupportedOSPlatform attribute can't be added by the SDK. In this case, you could see warnings for a platform-specific APIs usage even if you're targeting that platform. To resolve the warnings, enable the AssemblyInfo.cs file generation or add the attribute manually in your project.

Violations

Note

If you're building an app that doesn't target the unsupported platform, you won't get any violations. A violation only occurs in the following cases:

How to fix violations

The recommended way to deal with violations is to make sure you only call platform-specific APIs when running on an appropriate platform. You can achieve this by excluding the code at build time using #if and multi-targeting, or by conditionally calling the code at run time. The analyzer recognizes the platform guards in the OperatingSystem class and System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform.

When to suppress warnings

Referencing platform-specific APIs without a proper platform context or guard is not recommended. However, you can suppress these diagnostics using #pragma or the NoWarn compiler flag, or by setting the rule's severity to none in an .editorconfig file.

[SupportedOSPlatform("linux")]
public void LinuxOnlyApi() { }

public void Caller()
{
#pragma warning disable CA1416
    LinuxOnlyApi();
#pragma warning restore CA1416
}
Configure code to analyze

The analyzer is enabled by default only for projects that target .NET 5 or later and have an AnalysisLevel of 5 or higher. You can enable it for target frameworks lower than net5.0 by adding the following key-value pair to an .editorconfig file in your project:

dotnet_code_quality.enable_platform_analyzer_on_pre_net5_target = true
See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4