A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1421 below:

CA1421: Method uses runtime marshalling when 'DisableRuntimeMarshallingAttribute' is applied - .NET

CA1421: Method uses runtime marshalling when DisableRuntimeMarshallingAttribute is applied

Property Value Rule ID CA1421 Title Method uses runtime marshalling when DisableRuntimeMarshallingAttribute is applied Category Interoperability Fix is breaking or non-breaking Non-breaking Enabled by default in .NET 9 As suggestion Cause

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:

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

[assembly: DisableRuntimeMarshalling]

class C
{
    public void Test()
    {
        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

To fix the violation, remove the DisableRuntimeMarshallingAttribute attribute on the assembly.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4