YellowDogMan.Verify.Marshaling 0.2.0

Prefix Reserved
dotnet add package YellowDogMan.Verify.Marshaling --version 0.2.0
                    
NuGet\Install-Package YellowDogMan.Verify.Marshaling -Version 0.2.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="YellowDogMan.Verify.Marshaling" Version="0.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="YellowDogMan.Verify.Marshaling" Version="0.2.0" />
                    
Directory.Packages.props
<PackageReference Include="YellowDogMan.Verify.Marshaling" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add YellowDogMan.Verify.Marshaling --version 0.2.0
                    
#r "nuget: YellowDogMan.Verify.Marshaling, 0.2.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package YellowDogMan.Verify.Marshaling@0.2.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=YellowDogMan.Verify.Marshaling&version=0.2.0
                    
Install as a Cake Addin
#tool nuget:?package=YellowDogMan.Verify.Marshaling&version=0.2.0
                    
Install as a Cake Tool

Verify.Marshaling

When performing C# Interop/PInvoke with C++, you sometimes need to send structs from managed memory, to unmanaged memory.

In these cases .NET will perform Marshaling for you on your data. You can customize this marshaling with attributes.

Verify.Marshaling allows you to perform Snapshot Testing on the memory layouts of your structs.

This can be useful in order to compare them to their C++ equivalents, or to lock their layout between versions of a library.

Example

Given a struct:

[StructLayout(LayoutKind.Sequential)]
public struct SimpleStruct
{
    [MarshalAs(UnmanagedType.I4)]
    public int Test;

    [MarshalAs(UnmanagedType.R4)]
    public float TestF;
}

You can verify its layout with:

[TestMethod]
public async Task BasicTest()
{
    await VerifyMemoryLayout(typeof(SimpleStruct));
}

Which should produce an output like:

{
  FieldName: SimpleStruct,
  Size: 8,
  Nested: [
    {
      FieldName: Test,
      Size: 4,
      Type: Int32
    },
    {
      FieldName: TestF,
      Size: 4,
      Offset: 4,
      Type: Single
    }
  ],
  Type: struct
}

That can be saved as a part of your Snapshot collection, in the same way as Verify

Setup

If you're new to Verify, we recommend checking out the Verify getting started wizard.

Future Plans

Diagrams

Projects like StructLayout and even Visual Studio. Generate visual diagrams of a struct's layout.

Tooling to generate these for C# may be handy as an alternative method to verify the layout.

Formats

There are a number of tools for C++ that can examine memory layouts.

Matching these formats, may allow for automated tests that produce C++ sourced CLang dumps, that we can then compare with Verify.Marshaling snapshots. We could also convert between them.

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.0 99 7/13/2025
0.1.2 96 7/13/2025

Initial release