microsoft / CsWin32

A source generator to add a user-defined set of Win32 P/Invoke methods and supporting types to a C# project.
MIT License
2.06k stars 87 forks source link

Using attributes as an alternative to NativeMethods.txt #883

Closed vpenades closed 1 year ago

vpenades commented 1 year ago

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

I find using NativeMethods.txt to be hard to use because on complex scenarios where you have multiple classes using overlapping sets of native methods, it's hard to track what's in use and what's not.

Describe the solution you'd like

As an alternative to declaring NativeMethods.txt , I would like to use this approach:


[Shell32Import( "GetIconInfo", "DestroyIcon")]
[User32Import("*")] // import all user32 methods
static class SomeWrapperClass
{
}

This has the advantage that if you have multiple projects using CsWin32, you can copy a class decorated with these attributes from one project to another, without requiring to manually tailor a NativeMethods.txt. The library would simply scan all the classes using these attributes and would derive the set of methods in use by all the classes.

Describe alternatives you've considered

Additional context

AArnott commented 1 year ago

It's a neat idea. And perhaps at some point when things stabilize we can take the time to deliver this. But at this point we're not planning on this. Please keep the feedback coming.