[DllImportGenerator] Warn/fail when passing by reference a SafeHandle
without an accessible parameterless constructor.
#63109
Labels
area-System.Runtime.InteropServices
code-analyzer
Marks an issue that suggests a Roslyn analyzer
source-generator
Indicates an issue with a source generator feature
Milestone
When passing
SafeHandle
s by reference, theDllImportGenerator
needs their types to have an accessible parameterless constructor. If they don't, it tries to create them by generating a call toActivator.CreateInstance(typeof(MySafeHandle), nonPublic: true)
. If they don't have a parameterless constructor at all, the P/Invoke fails at runtime.An analyzer should be added that helps with this situation. If the
SafeHandle
descendant in question does not have an accessible parameterless constructor. If it is in the same assembly with the P/Invoke, it should cause an error, directing the user to add one (or even helping them with a fixer). If it is in a different assembly, it should warn the user that the P/Invoke is not guaranteed to work, and suggest them to contact theSafeHandle
in question's author.The text was updated successfully, but these errors were encountered: