A method uses runtime marshalling, and runtime marshalling is explicitly disabled.
Rule description
If a method uses runtime marshalling when runtime marshalling is disabled, it can cause unexpected behavior differences at run time due to different expectations of a type's native layout.
How to fix violations
Enable runtime marshalling or use features like sizeof and pointers to ensure accurate results.
When to suppress warnings
Don't suppress a warning from this rule.
Example
The following code snippet shows a violation of CA1421:
C#
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
[assembly: DisableRuntimeMarshalling]
classC
{
publicvoidTest()
{
nint offset = Marshal.OffsetOf(typeof(ValueType), "field");
}
}
struct ValueType
{
int field;
}
Imports System.Runtime.CompilerServices
Imports System.Runtime.InteropServices
<Assembly: DisableRuntimeMarshalling>
Class C
Shared Sub S1()
Dim offset As IntPtr = Marshal.OffsetOf(GetType(ValueType), "field")
End Sub
End Class
Structure ValueType
Dim field As Integer
End Structure
Šī satura avotu var atrast vietnē GitHub, kur varat arī izveidot un pārskatīt problēmas un atgādāšanas pieprasījumus. Lai iegūtu papildinformāciju, skatiet mūsu līdzstrādnieku rokasgrāmatu.
.NET atsauksmes
.NET ir atklātā pirmkoda projekts. Atlasiet saiti, lai sniegtu atsauksmes:
Pievienojieties meetup sērijai, lai kopā ar citiem izstrādātājiem un ekspertiem izveidotu mērogojamus AI risinājumus, kuru pamatā ir reālas lietošanas gadījumi.