次の方法で共有


CA1420: プロパティ、型、または属性にはランタイム マーシャリングが必要です

プロパティ
ルール ID CA1420
Title プロパティ、型、または属性にはランタイム マーシャリングが必要です
[カテゴリ] 相互運用性
修正が中断ありか中断なしか あり
.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 を削除します。