CA1420: Property, type, or attribute requires runtime marshalling
Property | Value |
---|---|
Rule ID | CA1420 |
Title | Property, type, or attribute requires runtime marshalling |
Category | Interoperability |
Fix is breaking or non-breaking | Breaking |
Enabled by default in .NET 8 | As warning |
Cause
A code feature is used that requires runtime marshalling, and runtime marshalling is explicitly disabled.
Rule description
Using features that require runtime marshalling when runtime marshalling is disabled will result in run-time exceptions.
How to fix violations
Enable runtime marshalling or remove the code that requires runtime marshalling.
When to suppress warnings
Don't suppress a warning from this rule.
Example
The following code snippet shows a violation of 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
To fix the violation, remove the DisableRuntimeMarshallingAttribute on the assembly.
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for