Historically, passing SafeHandle-derived types to P/Invokes and COM methods has implicitly required a parameterless constructor of any visibility when a SafeHandle-derived type is passed as a ref or out parameter or a return type. Source-generated interop in .NET 7 allowed this behavior to enable easier migration from DllImportAttribute-based P/Invokes. At the same time, we updated the SafeHandle documentation to tell implementers to provide a public parameterless constructor in their derived type. This breaking change makes that recommendation a requirement for source-generated marshalling.
Previous behavior
A SafeHandle-derived type was required to have a parameterless constructor of any visibility when it was used:
As a ref or out parameter or a return type in a LibraryImportAttribute-attributed method.
The interop source generators are changing to push more code out of the source generators themselves and into the core .NET libraries. As part of this change, the interop team is starting to enforce the recommended guidelines for more maintainable and understandable interop code.
Recommended action
Change the existing non-public parameterless constructor on the SafeHandle-derived type to be public.
Μπορείτε να βρείτε την πηγή για αυτό το περιεχόμενο στο GitHub, όπου μπορείτε επίσης να δημιουργήσετε και να εξετάσετε ζητήματα και αιτήματα έλξης. Για περισσότερες πληροφορίες, ανατρέξτε στον οδηγό συνεργατών.
.NET σχόλια
.NET είναι ένα έργο ανοιχτού κώδικα. Επιλέξτε μια σύνδεση για να παρέχετε σχόλια:
Συμμετάσχετε στη σειρά meetup για να δημιουργήσετε κλιμακούμενες λύσεις AI που βασίζονται σε πραγματικές περιπτώσεις χρήσης με συναδέλφους προγραμματιστές και ειδικούς.