Skip to content

Allow specifying extension member target for static class #1477

@JeremyKuhne

Description

@JeremyKuhne

Is your feature request related to a problem? Please describe.

When composing multiple assemblies that use CsWin32 you need to have multiple definitions of the main static class. In WinForms this ends up being PInvokeCore and PInvoke. This makes discovery difficult.

Describe the solution you'd like

Allow specifying an extension receiver class name that will result in the static class generating a specified extension scope.

In the NativeMethods.json:

{
  "$schema": "https://aka.ms/CsWin32.schema.json",
  "className": "WinFormsPInvokes",
  "extensionReciever": "PInvoke"
}

Generates (effectively):

namespace Windows.Win32;

internal partial static class WinFormsPInvokes
{
    extension (PInvoke)
    {
        // All defines generated here
    }
}

This feature requires C# 14 (with .NET 10) to compile but works on both .NET Framework and .NET (core). You can still reference the members directly via the static class name so consuming libraries can still use this without using C# 14. I don't think it is necessary to add any deduplication logic as part of this initial feature request.

Documentation:

https://learn.microsoft.com/dotnet/csharp/whats-new/csharp-14#extension-members
https://learn.microsoft.com/dotnet/csharp/language-reference/proposals/csharp-14.0/extensions

Describe alternatives you've considered

There aren't any other good options that I'm aware of.

Additional context

Add any other context or screenshots about the feature request here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions