4 ms·
I know this is a bit offtopic but a lot of people have worse experience with using (otherwise excellent) PInvoke in .NET than strictly necessary. > C# does not
by neonsunset 2y ago
I know this is a bit offtopic but a lot of people have worse experience with using (otherwise excellent) PInvoke in .NET than strictly necessary.
> C# does not allow directly using a C header file
https://github.com/dotnet/ClangSharp?tab=readme-ov-file#generating-bindings https://github.com/dotnet/ClangSharp?tab=readme-ov-file#gene...
there are bespoke libraries which build on top of it like CsWin32 where you specify the methods/modules to import and get nice and, often, memory-safe types and members in C#.
I think it should be possible to enhance this even further like having '// pinvkgen: #include <some_dependency.h>' at the top of any particular C# file and getting what you need in a C-like way. There are some problems with this inline approach but it could work.
The main point is there are quite a few community packages which simplify authoring bindings (there are more, like https://github.com/Cysharp/csbindgen/ https://github.com/Cysharp/csbindgen/ for Rust or https://github.com/royalapplications/beyondnet https://github.com/royalapplications/beyondnet for Swift). It really shouldn't be a necessity to write bindings by hand for large dependencies as it's both error prone and a solved problem.