共用方式為


CA1516:使用跨平台內部函數

Property Value
規則識別碼 CA1516
Title 使用跨平台內部函數
Category Maintainability
修正是破壞性或非破壞性 Non-breaking
在 .NET 10 中預設啟用 No

Cause

當跨平臺對等專案存在時,會使用平臺或架構特定的內部函數。

規則描述

此規則會偵測平臺特定內部函數的使用方式,這些內部函數可以改為取代為對等的跨平台內部函數。

如何修正違規

套用修正程式,以切換程式代碼以使用對等的跨平台內部函數。

Example

下列代碼段顯示 CA1516 的三個類似違規:

using System;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;
using System.Runtime.Intrinsics.Wasm;
using System.Runtime.Intrinsics.X86;

class C
{
    Vector128<byte> M1(Vector128<byte> x, Vector128<byte> y) => AdvSimd.Add(x, y);
    Vector128<byte> M2(Vector128<byte> x, Vector128<byte> y) => Sse2.Add(x, y);
    Vector128<byte> M3(Vector128<byte> x, Vector128<byte> y) => PackedSimd.Add(x, y);
}

下列代碼段會修正違規,並由修正程式套用:

using System;
using System.Runtime.Intrinsics;

class C
{
    Vector128<byte> M1(Vector128<byte> x, Vector128<byte> y) => x + y;
    Vector128<byte> M2(Vector128<byte> x, Vector128<byte> y) => x + y;
    Vector128<byte> M3(Vector128<byte> x, Vector128<byte> y) => x + y;
}

套用修正之後,很明顯,這三種方法可以簡化為適用於所有平臺上的單一方法。

隱藏警告的時機

如果您不擔心程式代碼的可維護性,則隱藏此規則的違規是安全的。

隱藏警告

如果您只想要隱藏單一違規,請將預處理器指示詞新增至原始程式檔以停用,然後重新啟用規則。

#pragma warning disable CA1516
// The code that's violating the rule is on this line.
#pragma warning restore CA1516

若要停用檔案、資料夾或項目的規則,請在none中將其嚴重性設為

[*.{cs,vb}]
dotnet_diagnostic.CA1516.severity = none

如需詳細資訊,請參閱 如何隱藏程式代碼分析警告

設定程式代碼以分析

您可以設定要套用此規則的 輸出元件類型 。 例如,若要只將此規則套用至產生主控台應用程式或動態連結庫的程式代碼(也就是UI應用程式),請將下列機碼/值組新增至 專案中的 .editorconfig 檔案:

dotnet_code_quality.CA1516.output_kind = ConsoleApplication, DynamicallyLinkedLibrary

如需詳細資訊,請參閱 output_kind