CA1420:屬性、型別或屬性需要執行階段封送處理

屬性
規則識別碼 CA1420
標題 屬性、類型或屬性需要執行時間封送處理
類別 互通性
修正程式是中斷或非中斷 中斷
預設在 .NET 8 中啟用 作為警告

原因

使用需要執行時間封送處理的程式碼功能,且明確停用執行時間封送處理。

檔案描述

停用執行階段封送處理時,使用需要執行階段封送處理的功能會導致執行階段例外狀況。

如何修正違規

啟用執行時間封送處理,或移除需要執行時間封送處理的程式碼。

隱藏警告的時機

請勿隱藏此規則的警告。

範例

下列程式碼片段顯示 CA1420 的違規:

using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

[assembly: DisableRuntimeMarshalling]

class C
{
    // Violates rule CA1420.
    [DllImport("NativeLibrary", SetLastError = true)]
    public static extern void MyMethod ();
}
Imports System.Runtime.InteropServices
Imports System.Runtime.CompilerServices

<Assembly: DisableRuntimeMarshalling>

Class C
    ' Violates rule CA1420.
    <DllImport("NativeLibrary", SetLastError:=True)>
    Public Shared Sub MyMethod()
        '...
    End Sub
End Class

若要修正違規,請移除 DisableRuntimeMarshallingAttribute 元件上的 。