共用方式為


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

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

原因

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

檔案描述

若使用需要執行時編組的功能,而執行時編組已停用,則會導致執行時例外。

如何修正違規

啟用運行時間封送處理,或移除需要運行時間封送處理的程序代碼。

隱藏警告的時機

請勿隱藏此規則的警告。

範例

下列代碼段顯示 CA1420 的違規:

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

[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 元件上的 。