IsBlittableGenerator 1.0.0

dotnet add package IsBlittableGenerator --version 1.0.0
                    
NuGet\Install-Package IsBlittableGenerator -Version 1.0.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="IsBlittableGenerator" Version="1.0.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="IsBlittableGenerator" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="IsBlittableGenerator">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
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 IsBlittableGenerator --version 1.0.0
                    
#r "nuget: IsBlittableGenerator, 1.0.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 IsBlittableGenerator@1.0.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=IsBlittableGenerator&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=IsBlittableGenerator&version=1.0.0
                    
Install as a Cake Tool

IsBlittableGenerator

A Roslyn source generator that automatically adds an IsBlittable property to structs with StructLayout attribute.

Description

IsBlittableGenerator is a .NET source generator that analyzes structs with StructLayout attribute and automatically adds a static IsBlittable property to determine if the struct is blittable.

This is particularly useful for interop scenarios where you need to know if a struct can be safely marshaled between managed and unmanaged code.

Features

  • Automatically generates IsBlittable property for structs with StructLayout attribute
  • Analyzes struct fields to determine blittability
  • Generates a static dictionary of blittable types for quick lookup
  • Supports partial structs
  • Works with .NET Standard 2.0 and above

Installation

<PackageReference Include="IsBlittableGenerator" Version="1.0.0" />

Usage

  1. Add the StructLayout attribute to your struct:
[StructLayout(LayoutKind.Sequential)]
internal partial struct MyStruct1
{
    public int Field1;
    public float Field2;
}

[StructLayout(LayoutKind.Sequential)]
internal partial struct MyStruct2
{
    public int Field1;
    public float Field2;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    public string Field3;
}

[StructLayout(LayoutKind.Sequential)]
internal partial struct MyStruct3
{
    public int Field1;
    public MyStruct1 Field2;
}

[StructLayout(LayoutKind.Sequential)]
internal partial struct MyStruct4
{
    public int Field1;
    public MyStruct5 Field2;
}

internal partial struct MyStruct5
{
    public int Field1;
    public float Field2;
}
  1. The generator will automatically add an IsBlittable property:
internal partial struct MyStruct1
{
    public static bool IsBlittable
    {
        get
        {
            return true;
        }
    }
}

internal partial struct MyStruct2
{
    public static bool IsBlittable
    {
        get
        {
            return false;
        }
    }
}

internal partial struct MyStruct3
{
    public static bool IsBlittable
    {
        get
        {
            return true;
        }
    }
}

internal partial struct MyStruct4
{
    public static bool IsBlittable
    {
        get
        {
            return false;
        }
    }
}
  1. You can also use the generated BlittableTypes class to check blittability:
bool isBlittable = IsBlittableGenerator.BlittableTypes.IsBlittable<MyStruct1>();

For example:

public unsafe static bool TryConvertTo<T>(IntPtr ptr, [MaybeNullWhen(false)] out T? value) where T : struct
{
    if(ptr == IntPtr.Zero)
    {
        value = null;
        return false;
    }

    if(IsBlittableGenerator.BlittableTypes.IsBlittable<T>())
    {
        value = System.Runtime.CompilerServices.Unsafe.AsRef<T>(ptr.ToPointer());
        return true;
    }

    value = Marshal.PtrToStructure<T>(ptr);
    return true;
}

PS: This is also the original intention of my writing this SG component

Requirements

  • .NET Standard 2.0 or later
  • C# 9.0 or later

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

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
1.0.0 124 4/25/2025